use*_*370 6 c++ rvalue-reference c++11
如果我定义一个接受rvalue引用参数的函数:
template <typename T>
void fooT(T &&x) {}
Run Code Online (Sandbox Code Playgroud)
我可以调用它,使用GCC 4.5,无论使用哪种a,ar或者arr:
int a, &ar = a, &&arr = 7;
fooT(a); fooT(ar); fooT(arr);
Run Code Online (Sandbox Code Playgroud)
但是,调用类似的非模板函数,
void fooInt(int &&x) {}
Run Code Online (Sandbox Code Playgroud)
这三个参数中的任何一个都会失败.我正准备加强我的知识forward,但这已经让我失去了理智.也许它是GCC 4.5; 我很惊讶地发现Rvalue References简介中的第一个例子也给出了一个编译错误:
A a;
A&& a_ref2 = a; // an rvalue reference
Run Code Online (Sandbox Code Playgroud)
除了GMan的正确答案之外,Rvalue References简介有一个不正确的例子,因为它是在语言变更之前编写的,它是非法的:
A a;
A&& a_ref2 = a; // an rvalue reference (DISALLOWED in C++11)
Run Code Online (Sandbox Code Playgroud)
尽管语言发生了这种变化,文章中描述的主要用例(移动和前进)仍然在文章中正确解释.
更新:哦,同一篇文章最初发布在这里与(imho)略微更好的格式.
| 归档时间: |
|
| 查看次数: |
3125 次 |
| 最近记录: |