Rectangle 27 1

c++ Is the result of a cast an rvalue?


The key is that there is no rule making an exception for a cast from T to 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.

Yes, the result of a cast to an object type is an rvalue, as specified by C++11 5.4/1:

Note
Rectangle 27 1

c++ Is the result of a cast an rvalue?


/Za
5.4

+1 for your recommendation. how long does the live sharing link lives?

@MinimusHeximus not sure, I have only used for short periods and th site does not document it.

As an aside, I would recommend using rextester over webcompiler since rextester allows you to share your program and also has live sharing.

Both gcc and clang result in rvalue which is the expected result.

I can only hypothesize that (char) i = 5; is more convenient to write than *((char*)&i) = 5; (in C). You can find some discussion when searching for "cast-as-lvalue". E.g. it seems to have been in early C89 drafts.

Mgetz filed a bug report, the response was that this is fixed by using the /Zc:rvalueCast flag, the description of the flag is as follows:

The language extension that binds an rvalue to an lvalue-reference only works if there is an object; that's not true for prvalues of fundamental types rextester.com/TOJWJL48414. Strangely, gcc once had a similar "extension" that allowed the result of a cast to be used as an lvalue: gcc.gnu.org/gcc-3.4/changes.html (cast-as-lvalue)

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 ]

When the /Zc:rvalueCast option is specified, the compiler correctly identifies an rvalue reference type as the result of a cast operation in accordance with the C++11 standard. When the option is not specified, the compiler behavior is the same as in Visual Studio 2012. By default, /Zc:rvalueCast is off. For conformance and to eliminate errors in the use of casts, we recommend that you use /Zc:rvalueCast.

Note