如何使模板右值引用参数仅绑定到右值引用?

问题描述 投票:27回答:5

我正在编写一个网络库,并大量使用移动语义来处理文件描述符的所有权。我的一个类希望接收其他类型的文件描述符包装并取得所有权,所以就像这样

struct OwnershipReceiver
{
  template <typename T>
  void receive_ownership(T&& t)
  {
     // taking file descriptor of t, and clear t
  }
};

它必须处理多个不相关的类型,所以receive_ownership必须是一个模板,为了安全起见,我希望它只绑定到右值引用,这样用户在传递左值时必须显式地声明std :: move。

receive_ownership(std::move(some_lvalue));

但问题是:C ++模板推导允许在没有额外努力的情况下传递左值。我实际上是因为不小心将左值传递给receive_ownership并且稍后使用了左值(清除)而将自己击中一只脚。

所以这是一个问题:如何使模板仅绑定到右值参考?

c++ rvalue-reference
5个回答
33
投票

您可以将T限制为不是左值引用,从而防止左值绑定到它:

#include <type_traits>

struct OwnershipReceiver
{
  template <typename T,
            class = typename std::enable_if
            <
                !std::is_lvalue_reference<T>::value
            >::type
           >
  void receive_ownership(T&& t)
  {
     // taking file descriptor of t, and clear t
  }
};

T添加某种限制以使其仅接受文件描述符包装器也可能是个好主意。


9
投票

一种简单的方法是提供一个接受左值引用的已删除成员:

template<typename T> void receive_ownership(T&) = delete;

这将始终是lvalue参数的更好匹配。


如果你有一个带有多个参数的函数,所有这些参数都需要是rvalues,我们需要几个已删除的函数。在这种情况下,我们可能更喜欢使用SFINAE来隐藏任何左值参数的函数。

一种方法可以使用C ++ 17和Concepts TS:

#include <type_traits>

template<typename T>
void receive_ownership(T&& t)
    requires !std::is_lvalue_reference<T>::value
{
     // taking file descriptor of t, and clear t
}

要么

#include <type_traits>

void receive_ownership(auto&& t)
    requires std::is_rvalue_reference<decltype(t)>::value
{
     // taking file descriptor of t, and clear t
}

稍微进一步,您可以定义自己的新概念,如果您想重复使用它,或者仅为了更加清晰:

#include <type_traits>

template<typename T>
concept bool rvalue = std::is_rvalue_reference<T&&>::value;


void receive_ownership(rvalue&& t)
{
     // taking file descriptor of t, and clear t
}

注意:使用GCC 6.1,您需要将-fconcepts传递给编译器,因为它是C ++ 17的扩展而不是它的核心部分。

为了完整起见,这是我的简单测试:

#include <utility>
int main()
{
    int a = 0;
    receive_ownership(a);       // error
    receive_ownership(std::move(a)); // okay

    const int b = 0;
    receive_ownership(b);       // error
    receive_ownership(std::move(b)); // allowed - but unwise
}

4
投票

我学到的东西似乎经常让人迷惑:使用SFINAE是可以的,但我不能使用:

std::is_rvalue_reference<T>::value

它按我想要的唯一方式是

!std::is_lvalue_reference<T>::value

原因是:我需要我的函数来接收rvalue,而不是rvalue引用。使用std::is_rvalue_reference<T>::value有条件启用的函数不会接收rvalue,而是接收rvalue引用。


2
投票

对于左值引用,T被推导为左值引用,对于右值引用,T被推断为非引用。

因此,如果函数绑定到右值引用,编译器最终在某个类型T中看到的是:

std::is_rvalue_reference<T>::value

并不是

std::is_rvalue_reference<T&&>::value


0
投票

不幸的是,似乎尝试is_rvalue_reference<TF>(其中TF是完美转发的类型)如果你实际上尝试制作区分const T&T&&的重载(例如在两者中使用enable_if,一个与is_rvalue_reference_v<TF>和另一个与!is_rvalue_reference_V<TF>)。

一个解决方案(尽管是hacky)是衰减转发的T,然后将重载放在一个知道这些类型的容器中。生成的this example

Hup,我错了,只是忘了看托比的回答(is_rvalue_reference<TF&&>) - 虽然你可以做std::forward<TF>(...)令人困惑,但我猜这就是为什么decltype(arg)也有效。

Anywho,这是我用于调试的内容:(1)使用struct重载,(2)使用错误的is_rvalue_reference检查,以及(3)正确检查:

/*
Output:

const T& (struct)
const T& (sfinae)
const T& (sfinae bad)
---
const T& (struct)
const T& (sfinae)
const T& (sfinae bad)
---
T&& (struct)
T&& (sfinae)
const T& (sfinae bad)
---
T&& (struct)
T&& (sfinae)
const T& (sfinae bad)
---
*/

#include <iostream>
#include <type_traits>

using namespace std;

struct Value {};

template <typename T>
struct greedy_struct {
  static void run(const T&) {
    cout << "const T& (struct)" << endl;
  }
  static void run(T&&) {
    cout << "T&& (struct)" << endl;
  }
};

// Per Toby's answer.
template <typename T>
void greedy_sfinae(const T&) {
  cout << "const T& (sfinae)" << endl;
}

template <
    typename T,
    typename = std::enable_if_t<std::is_rvalue_reference<T&&>::value>>
void greedy_sfinae(T&&) {
  cout << "T&& (sfinae)" << endl;
}

// Bad.
template <typename T>
void greedy_sfinae_bad(const T&) {
  cout << "const T& (sfinae bad)" << endl;
}

template <
    typename T,
    typename = std::enable_if_t<std::is_rvalue_reference<T>::value>>
void greedy_sfinae_bad(T&&) {
  cout << "T&& (sfinae bad)" << endl;
}

template <typename TF>
void greedy(TF&& value) {
  using T = std::decay_t<TF>;
  greedy_struct<T>::run(std::forward<TF>(value));
  greedy_sfinae(std::forward<TF>(value));
  greedy_sfinae_bad(std::forward<TF>(value));
  cout << "---" << endl;
}

int main() {
  Value x;
  const Value y;

  greedy(x);
  greedy(y);
  greedy(Value{});
  greedy(std::move(x));

  return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.