rvalue

C++0x const RValue reference as function parameter

∥☆過路亽.° 提交于 2019-12-17 16:43:13
问题 I am trying to understand why someone would write a function that takes a const rvalue reference . In the code example below what purpose is the const rvalue reference function (returning "3"). And why does overload resolution preference the const Rvalue above the const LValue reference function (returning "2"). #include <string> #include <vector> #include <iostream> std::vector<std::string> createVector() { return std::vector<std::string>(); } //takes movable rvalue void func(std::vector<std

Is a member of an rvalue structure an rvalue or lvalue?

ⅰ亾dé卋堺 提交于 2019-12-17 16:13:29
问题 A function call returning a structure is an rvalue expression, but what about its members? This piece of code works well with my g++ compiler, but gcc gives a error saying "lvalue required as left operand of assignment": struct A { int v; }; struct A fun() { struct A tmp; return tmp; } int main() { fun().v = 1; } gcc treats fun().v as rvalue, and I can understand that. But g++ doesn't think the assignment expression is wrong. Does that mean fun1().v is lvalue in C++? Now the problem is, I

On OS X, simple C++ program gives incorrect results (which are a result of command-line options 'c++03' vs 'c++11')

独自空忆成欢 提交于 2019-12-17 13:56:19
问题 This simple program (when compiled on Linux) will CORRECTLY give two different answers based on whether it's compiled with -std=c++0x or not. Problem: I cannot reproduce the same thing on OS X (Mountain Lion, 10.8 SDK). What am I missing? #include <iostream> #include <sstream> class Thing : public std::ostringstream { public: Thing() : std::ostringstream() {} virtual ~Thing() { std::cerr << str(); } }; int main(int argc, const char * argv[]) { Thing() << "Hello" << std::endl; return 0; } To

Isn't the const modifier here unnecessary? [duplicate]

混江龙づ霸主 提交于 2019-12-17 06:07:53
问题 This question already has an answer here : How can a returned object be assignable? (1 answer) Closed 6 years ago . The " Effective C++ " Item 3 says "Use const whenever possible", and it gives an example like: const Rational operator*(const Rational& lhs, const Rational& rhs); to prevent clients from being able to commit atrocities like this: Rational a, b, c; ... (a * b) = c; // invoke operator= on the result of a*b! But isn't the non-reference return value of functions allready a rvalue ?

Isn't the const modifier here unnecessary? [duplicate]

℡╲_俬逩灬. 提交于 2019-12-17 06:07:02
问题 This question already has an answer here : How can a returned object be assignable? (1 answer) Closed 6 years ago . The " Effective C++ " Item 3 says "Use const whenever possible", and it gives an example like: const Rational operator*(const Rational& lhs, const Rational& rhs); to prevent clients from being able to commit atrocities like this: Rational a, b, c; ... (a * b) = c; // invoke operator= on the result of a*b! But isn't the non-reference return value of functions allready a rvalue ?

non-class rvalues always have cv-unqualified types

淺唱寂寞╮ 提交于 2019-12-17 04:29:08
问题 §3.10 section 9 says "non-class rvalues always have cv-unqualified types". That made me wonder... int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvalue\n"; } void pass_int(const int&& i) { std::cout << "const rvalue\n"; } int main() { pass_int(foo()); // prints "rvalue" pass_int(bar()); // prints "const rvalue" } According to the standard, there is no such thing as a const rvalue for non-class types, yet bar() prefers to bind to const int&& . Is

non-class rvalues always have cv-unqualified types

喜夏-厌秋 提交于 2019-12-17 04:29:02
问题 §3.10 section 9 says "non-class rvalues always have cv-unqualified types". That made me wonder... int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvalue\n"; } void pass_int(const int&& i) { std::cout << "const rvalue\n"; } int main() { pass_int(foo()); // prints "rvalue" pass_int(bar()); // prints "const rvalue" } According to the standard, there is no such thing as a const rvalue for non-class types, yet bar() prefers to bind to const int&& . Is

Should r values be expressions which evaluate to an expressed value or a storable value?

只谈情不闲聊 提交于 2019-12-13 02:55:46
问题 Essentials of Programming Languages says References are sometimes called L-values. This name reflects the association of such data structures with variables appearing on the left-hand side of assignment statements. Analogously, expressed values , such as the values of the right-hand side expressions of assignment statements, are known as R-values . while https://course.ccs.neu.edu/csg111/lectures/lec06.html from a course based on the book says References are sometimes called L-values because

Getting the address of an rvalue

末鹿安然 提交于 2019-12-12 13:48:28
问题 class MyClass { public: MyClass(int a) : a(a) { } int a; }; #include <iostream> void print(MyClass* a) { std::cout << a->a << std::endl; } int main() { print(&static_cast<MyClass&&>(MyClass(1337))); return 0; } This doesn't work with GCC 4.6, while it used to work in a previous version. Now it says: taking address of xvalue (rvalue reference). Is there any way to securely pass the address of an rvalue to another function? 回答1: is: there is anyway to securely pass an rvalue reference (a.k.a.

What constitutes of RValues?

不问归期 提交于 2019-12-12 03:04:02
问题 RValues are things which are not maniputable regions of memory, so literals like integers are considered RValues. Do constants constitute RValues? const int x = 0; is maniputable at least one time. Now, the temporary objects created by the compiler are also RValues even when they have maniputable memory regions. Why is that so? Because they cannot be modified by "users"? Is this the reason? So, a memory region which is NOT maniputable by the "users" is called RValue? 回答1: Scalar rvalues are