rvalue

What is decltype(0 + 0)?

和自甴很熟 提交于 2020-01-11 08:05:07
问题 (Prompted by an answer.) Given N3290, §7.1.6.2p4, where the list items are unnumbered, but numbered here for our convenience: The type denoted by decltype(e) is defined as follows: if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed; otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e

Why do some c++ compilers let you take the address of a literal?

半城伤御伤魂 提交于 2020-01-10 20:01:14
问题 A C++ compiler that I will not name lets you take the address of a literal, int *p = &42; Clearly 42 is an r-value and most compilers refuse to do so. Why would a compiler allow this? What could you do with this other than shoot yourself in the foot? 回答1: What if you needed a pointer to an integer with the value of 42? :) C++ references are much like automatically dereferenced pointers. One can create a constant reference to a literal, like this: const int &x = 42; It effectively requires the

One VS2010 bug ? Allowing binding non-const reference to rvalue WITHOUT EVEN a warning?

时光总嘲笑我的痴心妄想 提交于 2020-01-08 13:27:30
问题 string foo() { return "hello"; } int main() { //below should be illegal for binding a non-const (lvalue) reference to a rvalue string& tem = foo(); //below should be the correct one as only const reference can be bind to rvalue(most important const) const string& constTem = foo(); } GCC is the good one to give a compile error : invalid initialization of non-const reference of type std::string& from a temporary of type std::string VS2008 is not too bad as at least it gives a compile warning :

One VS2010 bug ? Allowing binding non-const reference to rvalue WITHOUT EVEN a warning?

自闭症网瘾萝莉.ら 提交于 2020-01-08 13:26:49
问题 string foo() { return "hello"; } int main() { //below should be illegal for binding a non-const (lvalue) reference to a rvalue string& tem = foo(); //below should be the correct one as only const reference can be bind to rvalue(most important const) const string& constTem = foo(); } GCC is the good one to give a compile error : invalid initialization of non-const reference of type std::string& from a temporary of type std::string VS2008 is not too bad as at least it gives a compile warning :

One VS2010 bug ? Allowing binding non-const reference to rvalue WITHOUT EVEN a warning?

荒凉一梦 提交于 2020-01-08 13:26:47
问题 string foo() { return "hello"; } int main() { //below should be illegal for binding a non-const (lvalue) reference to a rvalue string& tem = foo(); //below should be the correct one as only const reference can be bind to rvalue(most important const) const string& constTem = foo(); } GCC is the good one to give a compile error : invalid initialization of non-const reference of type std::string& from a temporary of type std::string VS2008 is not too bad as at least it gives a compile warning :

One VS2010 bug ? Allowing binding non-const reference to rvalue WITHOUT EVEN a warning?

[亡魂溺海] 提交于 2020-01-08 13:26:45
问题 string foo() { return "hello"; } int main() { //below should be illegal for binding a non-const (lvalue) reference to a rvalue string& tem = foo(); //below should be the correct one as only const reference can be bind to rvalue(most important const) const string& constTem = foo(); } GCC is the good one to give a compile error : invalid initialization of non-const reference of type std::string& from a temporary of type std::string VS2008 is not too bad as at least it gives a compile warning :

Does a Comparison Between an Lvalue and a Literal Invoke an Lvalue-to-Rvalue Conversion?

不打扰是莪最后的温柔 提交于 2020-01-02 08:33:08
问题 I asked this question: static_assert of const Variable And apparently it comes down to the question does a floating point lvalue get converted to an rvalue for the purposes of comparison? So in this code does an lvalue-to-rvalue conversion occur? const float foo = 13.0F; static_assert(foo > 0.0F, "foo must be greater than 0."); 回答1: Yes, it is performed. Basically, it's all because 3.0 > 1.2 is a well-formed expression, that contains nothing but prvalues for operands. First, [expr]/9 states

Why does std::reference_wrapper<const T> not accept a temporary?

早过忘川 提交于 2020-01-01 07:59:45
问题 Normally, rvalues can bind to const references ( const SomeType& ). It's built into the language. However, std::reference_wrapper<const T> does not accept an rvalue as its constructor argument since the corresponding overload is deliberately deleted. What is the reason for this inconsistency? std::reference_wrapper is "advertised" as the alternative to a reference variable for cases when we must pass by value but would like to preserve reference semantics. In other words, if the rvalue to

What is the reasoning behind the naming of “lvalue” and “rvalue”?

孤者浪人 提交于 2019-12-30 14:57:27
问题 What is the reasoning behind the naming of "lvalue" and "rvalue" in C/C++ (I know how they function)? 回答1: The standard mentions this: An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment expression) [...] An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment expression) [...] That is, an lvalue was something you could assign to and an rvalue was something you could assign from. However, this

Why are C++0x rvalue reference not the default?

被刻印的时光 ゝ 提交于 2019-12-29 14:16:37
问题 One of the cool new features of the upcoming C++ standard, C++0x, are "rvalue references." An rvalue reference is similar to an lvalue (normal) reference, except that it can be bound to a temporary value (normally, a temporary can only be bound to a const reference): void FunctionWithLValueRef(int& a) {...} void FunctionWithRValueRef(int&& a) {...} int main() { FunctionWithLValueRef(5); // error, 5 is a temporary FunctionWithRValueRef(5); // okay } So, why did they invent a whole new type,