当函数采用右值引用时,函数中该变量的类型是什么? [英] When a function takes an rvalue reference, what is the type of that variable within the function?
问题描述
这是一个术语问题.如果我有这个:
This is a question of terminology. If I have this:
#include <vector>
void g(std::vector<int>&& arg);
void f0(std::vector<int>&& v) {
static_assert(std::is_same<decltype(v), std::vector<int>&&>::value); // Looks like v is an rvalue reference.
static_assert(std::is_same<decltype((v)), std::vector<int>&>::value);
static_assert(std::is_same<std::decay<decltype(v)>::type, std::vector<int>>::value);
return g(std::move(v)); // Fine.
}
那么v
是什么类型?如果你在谈论调用 f0
,你会说f0
需要一个右值引用"(对吧?)但是在 f0
中,v
不是右值引用,否则不需要 std::move
?正确的?但是 static_assert
显示它是一个右值,对吗?
then what type is v
? If you are talking about calling f0
, you'd say "f0
takes an rvalue reference" (right?) but within f0
, v
isn't an rvalue reference, or else the std::move
wouldn't be required? Right? But the static_assert
showed that it is an rvalue, right?
同样:
void f1(std::vector<int>&& v) {
static_assert(std::is_same<decltype(v), std::vector<int>&&>::value);
static_assert(std::is_same<decltype((v)), std::vector<int>&>::value);
static_assert(std::is_same<std::decay<decltype(v)>::type, std::vector<int>>::value);
return g(v); // Error: cannot bind rvalue reference of type 'std::vector<int>&&' to lvalue of type 'std::vector<int>'.
// So is v just a std::vector<int>?
}
局部右值引用的作用相同:
Local rvalue references act the same way:
void f2(std::vector<int>&& v) {
std::vector<int>&& vv = std::move(v);
static_assert(std::is_same<decltype(vv), decltype(v)>::value, "They are the same decltype. So being an argument isn't magic.");
static_assert(std::is_same<decltype(vv), std::vector<int>&&>::value);
static_assert(std::is_same<decltype((vv)), std::vector<int>&>::value);
static_assert(std::is_same<std::decay<decltype(vv)>::type, std::vector<int>>::value);
return g(vv); // Error: cannot bind rvalue reference of type 'std::vector<int>&&' to lvalue of type 'std::vector<int>'
}
描述 v
类型的正确术语是什么?说 f0
使用右值引用是否正确?如果 v
是一个右值引用,那么用什么术语来说明不能使用右值引用来调用采用右值引用的函数?
What is the correct terminology to describe the type of v
? Is it correct to say f0
takes an rvalue reference? If v
is an rvalue reference, what's the terminology to say that an rvalue reference can't be used to call a function taking an rvalue reference?
推荐答案
v
变量的声明类型是 std::vector
.这种类型读作rvalue reference to std::vector
".
The declared type of the variable named v
is std::vector<int>&&
. This type is read "rvalue reference to std::vector
".
名称v
可以出现在表达式中.表达式从不具有引用类型 [expr.type]/1.但是表达式有一个值类别.当名称 v
出现在 v[0]
中的表达式中时,子表达式 v
的类型为 std::vector
,其值类别为lvalue.几乎所有的id都是这种情况-expression(只是一个名字的表达式).
The name v
can appears in an expression. Expressions never have reference type [expr.type]/1. But expressions have a value category. When the name v
appears in an expression as in v[0]
, the subexpression v
has type std::vector<int>
and its value category is lvalue. This is the case of almost all id-expression (expressions that are just a name).
decltype(v)
给出变量 v
的声明类型.
decltype(v)
gives the declared type of the variable v
.
decltype(expression)
给出:
- 一个对
expression
类型的左值引用,如果expression
是一个左值, - 一个对
expression
类型的右值引用,如果expression
是一个xvalue, expression
的类型,如果expression
是一个纯右值.
- a lvalue-reference to the type of
expression
ifexpression
is a lvalue, - a rvalue-reference to the type of
expression
ifexpression
is a xvalue, - the type of
expression
ifexpression
is a prvalue.
更多细节在[dcl.dcl]/1中给出一>.
这篇关于当函数采用右值引用时,函数中该变量的类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!