rvalue

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

放肆的年华 提交于 2019-11-26 15:31:24
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 : warning C4239: nonstandard extension used : 'initializing' : conversion from std::string to std::string

Why is ++i considered an l-value, but i++ is not?

我只是一个虾纸丫 提交于 2019-11-26 15:06:12
Why is ++i is l-value and i++ not? Johannes Schaub - litb Well as another answerer pointed out already the reason why ++i is an lvalue is to pass it to a reference. int v = 0; int const & rcv = ++v; // would work if ++v is an rvalue too int & rv = ++v; // would not work if ++v is an rvalue The reason for the second rule is to allow to initialize a reference using a literal, when the reference is a reference to const: void taking_refc(int const& v); taking_refc(10); // valid, 10 is an rvalue though! Why do we introduce an rvalue at all you may ask. Well, these terms come up when building the

On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule

青春壹個敷衍的年華 提交于 2019-11-26 12:43:35
问题 I was reading Thomas Becker\'s article on rvalue reference and their use. In there he defines what he calls if-it-has-a-name rule: Things that are declared as rvalue reference can be lvalues or rvalues. The distinguishing criterion is: if it has a name, then it is an lvalue. Otherwise, it is an rvalue. This sounds very reasonable to me. It also clearly identifies the rvalueness of an rvalue reference. My questions are: Do you agree with this rule? If not, can you give an example where this

Passing rvalues through std::bind

会有一股神秘感。 提交于 2019-11-26 11:05:14
问题 I want to pass an rvalue through std::bind to a function that takes an rvalue reference in C++0x. I can\'t figure out how to do it. For example: #include <utility> #include <functional> template<class Type> void foo(Type &&value) { Type new_object = std::forward<Type>(value); // move-construct if possible } class Movable { public: Movable(Movable &&) = default; Movable &operator=(Movable &&) = default; }; int main() { auto f = std::bind(foo<Movable>, Movable()); f(); // error, but want the

Is it valid to bind non-const lvalue-references to rvalues in C++ 11?(modified)

此生再无相见时 提交于 2019-11-26 09:57:24
问题 I know in c++03, an an non-const reference cannot be bound to rvalues. T& t = getT(); is invalid, and in c++11, we can do this: T&& t = getT(); but what about the above code, should that work in c++11? I tested the codes below with vs11: Foo getFoo() { return Foo(); } void fz(Foo& f) { } int getInt() { return int(); } void iz(int& i) { } int main() { { Foo& z = getFoo(); //ok fz(getFoo()); //ok int& z2 = getInt(); //error: initial value of reference to non-const must be an lvalue iz(getInt())

Do rvalue references allow dangling references?

旧街凉风 提交于 2019-11-26 09:23:31
问题 Consider the below. #include <string> using std::string; string middle_name () { return \"Jaan\"; } int main () { string&& danger = middle_name(); // ?! return 0; } This doesn\'t compute anything, but it compiles without error and demonstrates something that I find confusing: danger is a dangling reference, isn\'t it? 回答1: Do rvalue references allow dangling references? If you meant "Is it possible to create dangling rvalue references" then the answer is yes. Your example, however, string

Why are rvalues references variables not rvalue?

不羁的心 提交于 2019-11-26 08:32:15
问题 Let\'s say I have two overloads of a function f . f(T&&) and f(T&) . Then in the body of g : g(T&& t) { f(t);} the overload f(T&) will be called because t is considered an lvalue. This is very surprising to me. How a function with signature f(T&&) can not match a call with type T&& ? What suprises me even more is that a call f(static_cast<T&&>(t)) would actually call the rvalue overload f(T&&) . What are the C++ rules that make this possible? Is T&& more than a type? 回答1: The things that are

What is the value category of the operands of C++ operators when unspecified?

我的未来我决定 提交于 2019-11-26 06:48:24
问题 PREMISE: The C++11 Standard classifies expressions into three disjoint value categories : lvalues , xvalues , and prvalues (§ 3.10/1). An explanation of what value categories are is available for instance here. I am struggling to figure out what are the requirements of the different operators on the value category of their operands. Paragraph 3.10/1 specifies: [...] Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue. This

In C++, what categories (lvalue, rvalue, xvalue, etc.) can expressions that produce temporaries of class type fall into?

南笙酒味 提交于 2019-11-26 06:41:47
问题 Here is some example code: #include <iostream> class Foo { public: explicit Foo(int x) : data(x) {}; Foo& operator++() { data += 1; return *this; } void *get_addr() { return (void*)this; } friend Foo operator + (const Foo& lhs, const Foo& rhs); friend std::ostream& operator << (std::ostream& os, const Foo& f); private: int data; }; std::ostream& operator << (std::ostream& os, const Foo& f) { return (os << f.data); } Foo operator + (const Foo& lhs, const Foo& rhs) { return Foo(lhs.data + rhs

Taking the address of a temporary object

允我心安 提交于 2019-11-26 05:33:56
问题 §5.3.1 Unary operators, Section 3 The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id. What exactly does \"shall be\" mean in this context? Does it mean it\'s an error to take the address of a temporary? I was just wondering, because g++ only gives me a warning, whereas comeau refuses to compile the following program: #include <string> int main() { &std::string(\"test\"); } g++ warning : taking address of temporary comeau error :