6

gcc.godbolt.org上的代码

我创建了一个简单的类型特征来删除右值引用:

template <typename T>
struct remove_rvalue_reference { using type = T; };

template <typename T>
struct remove_rvalue_reference<T&&> { using type = T; };

template <typename T>
using remove_rvalue_reference_t = 
    typename remove_rvalue_reference<T>::type;

我用它来实现一个copy_if_rvalue(x)函数,它的返回类型取决于传递的参数:

template <typename T>
constexpr auto copy_if_rvalue(T && x) 
  -> remove_rvalue_reference_t<decltype(std::forward<decltype(x)>(x))>
{
    return std::forward<decltype(x)>(x);
}

为了确保函数返回正确的类型,我编写了一些简单的静态断言:

// literal
static_assert(std::is_same<
    decltype(copy_if_rvalue(0)), int
>{});

// lvalue
int lv = 10;
static_assert(std::is_same<
    decltype(copy_if_rvalue(lv)), int&
>{});

// const lvalue
const int clv = 10;
static_assert(std::is_same<
    decltype(copy_if_rvalue(clv)), const int&
>{});

// rvalue
int rv = 10;
static_assert(std::is_same<
    decltype(copy_if_rvalue(std::move(rv))), int
>{});

// volatile lvalue
volatile int vlv = 10;
static_assert(std::is_same<
    decltype(copy_if_rvalue(vlv)), volatile int&
>{});

// const lvalue
volatile const int vclv = 10;
static_assert(std::is_same<
    decltype(copy_if_rvalue(vclv)), volatile const int&

以上所有静态断言都编译成功。但是,当尝试std::move使用volatile int变量时,会发生一些意想不到的事情:

// volatile rvalue
volatile int vrv = 10;

// (0) fails:
static_assert(std::is_same<
    decltype(copy_if_rvalue(std::move(vrv))), volatile int
>{});

// (1) unexpectedly passes:
static_assert(std::is_same<
    decltype(copy_if_rvalue(std::move(vrv))), int
>{});

// (2) unexpectedly passes:
static_assert(std::is_same<
    remove_rvalue_reference_t<decltype(std::forward<decltype(vrv)>(std::move(vrv)))>, 
    volatile int
>{});

断言 (0) 失败 -volatile不传播,如断言 (1) 所示。

但是,断言 (2) 通过了,即使我认为它应该等同于断言 (0),因为copy_if_rvalue的返回类型与 (2) 的第一种类型完全相同:

// (from assertion (2))
remove_rvalue_reference_t<decltype(std::forward<decltype(vrv)>(std::move(vrv)))> 

// ...should be equivalent to...

// (from copy_if_rvalue)
-> remove_rvalue_reference_t<decltype(std::forward<decltype(x)>(x))>

似乎volatile仅在std::move使用时才传播,并且仅通过copy_if_rvalue模板函数传播。

这里发生了什么?

4

1 回答 1

4

没有 cv 限定的标量纯右值。[表达式]/6

如果 prvalue 最初具有类型“<em>cv T”,其中T是 cv 非限定的非类、非数组类型,则T在任何进一步分析之前将表达式的类型调整为。

即相同的规则给出

int const f();
f() // <=

typeint在这里也适用。如果您尝试使用某些类类型而不是int(例如std::string),您将获得预期的类型。

于 2016-04-27T15:36:55.990 回答