c++ 演員的結果是高價值嗎?
讓
int a = 0;
那麼(C)是標準C中的值?
不同的編譯器對此程式碼顯示不同的結果:
#include <iostream> using namespace std; void f(int& x) { cout << "l value" << endl; } void f(int&& x) { cout << "r value" << endl; } int main() { int a = 0; f((int)a); }
編譯器具有不同的結果:
1)ofollow,noindex" target="_blank">http://cpp.sh/2r6
)禁用.
我們可以看到,這應該是C標準草案5.4的顯著型別轉換(鑄造符號)第1段的重要值(特別是prvalue)(強調我的):
The result of the expression (T) cast-expression is of type T. The
result is an lvalue if T is an lvalue reference type or an rvalue
reference to function type and an xvalue if T is an rvalue reference
to object type;otherwise the result is a prvalue. [ Note: if T is
a non-class type that is cv-qualified, the cv-qualifiers are ignored
when determining the type of the resulting prvalue; see 3.10. —end
note ]
除此之外,我建議在web編譯器上使用rextester ,因為rextester可以讓您共享程式,並且還可以實時共享.
更新
Ben Voigt指出這個bug report ,所以似乎Visual Studio實際上產生了一個左值.所以這不僅僅是extension which allows temporary objects to be bound to non-const lvalue references 的一個例子.
因為dyp也指出了gcc used to have a cast to lvalue extension .
更新2
Mgetz提交了bug report ,答覆是這是通過使用/Zc:rvalueCast flag 修復的,標誌的描述如下:
When the /Zc:rvalueCast option is specified, the compiler correctlyidentifies an rvalue reference type as the result of a cast operationin accordance with the C++11 standard. When the option is notspecified, the compiler behavior is the same as in Visual Studio 2012.By default, /Zc:rvalueCast is off. For conformance and to eliminateerrors in the use of casts, we recommend that you use /Zc:rvalueCast.
這個標誌在未來的版本中是否預設啟用是不清楚的.
http://stackoverflow.com/questions/26508609/is-the-result-of-a-cast-an-rvalue