What rvalues have names?

和自甴很熟 提交于 2019-12-23 07:01:25

问题


@FredOverflow mentioned in the C++ chatroom that this is a rare case of rvalues that have names. The C++0x FDIS mentions under 5.1.1 [expr.prim.general] p4:

Otherwise, if a member-declarator declares a non-static data member (9.2) of a class X, the expression this is a prvalue of type “pointer to X” within the optional brace-or-equal-initializer. It shall not appear elsewhere in the member-declarator. (emphasis mine)

What others are there, if any?


回答1:


One prominent case are enumerators

enum arity { one, two };

The expressions one and two are rvalues (more specifically, prvalues in C++0x). Another are template non-type parameters

template<int *P> struct A { };

The expression P is an rvalue too (more specifically again, a prvalue in C++0x).




回答2:


  1. The boolean literals true and false are prvalues of type bool.
  2. nullptr is a prvalue of type nullptr_t.
  3. When you return a named variable from a function, it becomes an xvalue in the context of that expression, and an xvalue is an rvalue (per §3.10/1).

There may be more, but those are all I can think of at the moment (and the third is questionable -- it's really the expression that's the xvalue, but with something like return x; (where x is a local variable and you're returning the value, not a reference), the name of the variable is the expression. The name really refers to a glvalue, and in the expression that value (but not really the name) gets converted to an xvalue (which is an rvalue).



来源:https://stackoverflow.com/questions/6162226/what-rvalues-have-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!