rvalue

Why const for implicit conversion?

拥有回忆 提交于 2019-12-29 04:42:05
问题 After extensive reading of ISO/IEC 14882, Programming language – C++ I'm still unsure why const is needed for implicit conversion to a user-defined type with a single argument constructor like the following #include <iostream> class X { public: X( int value ) { printf("constructor initialized with %i",value); } } void implicit_conversion_func( const X& value ) { //produces "constructor initialized with 99" } int main (int argc, char * const argv[]) { implicit_conversion_func(99); } Starting

C++11 rvalue object field

陌路散爱 提交于 2019-12-25 07:10:02
问题 Can I have class/struct with rvalue field in c++11? Like this one: template<typename T> struct RvalueTest{ RvalueTest(T&& value) : value( std::forward<T>(value) ){} T&& value; }; Because in the following test: class Widget { public: Widget(){std::cout << "Widget ctor " << std::endl; } Widget(int h) : h(h){std::cout << "Widget ctor param " << std::endl; } Widget(const Widget&) { std::cout << "Widget copy ctor " << std::endl; } Widget(Widget&&) { std::cout << "Widget move ctor " << std::endl; }

What is LValues and RValues in objective c?

☆樱花仙子☆ 提交于 2019-12-25 03:22:59
问题 There are two kinds of expressions in Objective-C 1. RValue The term rvalue refers to a data value that is stored at some address in memory 2. LValue Expressions that refer to a memory location is called "lvalue" expression. An lvalue may appear as either the left-hand or right-hand side of an assignment I didn't get it . can someone explain it to me? 回答1: RValue is a value that is evaluated but does not have a designated memory address to be stored until assigned to such a memory location.

Assigning a volatile rvalue

若如初见. 提交于 2019-12-24 14:18:27
问题 I don't understand why the following code doesn't compile: #include <iostream> class Test { public: Test() { std::cout << "Constructor" << std::endl; } Test(const Test&) { std::cout << "Copy Constructor" << std::endl; } Test& operator=(const Test&) { std::cout << "Assign Op" << std::endl; return *this; } Test& operator=(const volatile Test&) { std::cout << "Volatile Assign Op" << std::endl; return *this; } }; volatile Test func() { Test a; return a; } int main() { Test b; volatile Test c; b =

Is it legal to take the address of a const lvalue reference?

僤鯓⒐⒋嵵緔 提交于 2019-12-23 15:41:33
问题 #include <iostream> int foo() { return 0; } int main() { const int& a = foo(); std::cout << &a << std::endl; } In this code, a binds to a rvalue. Is it legal to take its address? (And by legal I mean: in the code ill-formed? Am I causing an undefined behaviour?) 回答1: This is fine. In C++11 you can even do this: int&& a = foo(); a = 123; You can kind of think about temporaries like this (conceptually and in general): x = func(); // translated as: auto __temporary = func(); x = __temporary; _

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

C++ - how to return a prvalue by reference?

落花浮王杯 提交于 2019-12-23 04:26:47
问题 So I'm implementing a native arrays wrapper which will allow such to be passed as function arguments and to be returned. I'm having a trouble however with casting it to a native array as native arrays can't be returned. As an replacement I decided to use 'rvalue' reference return type of the casting operator but this will not act correctly because if I want to bind the returned object into an 'rvalue' reference in order to extend it's life-time this won't happen as it's an 'xvalue' and not

Why can an rvalue not bind to a non-const lvalue reference, other than the fact that writing to a temporary has no effect?

亡梦爱人 提交于 2019-12-22 11:12:06
问题 I have read the SO question here and understood this part of the answer: "But if you bind a temporary to a non-const reference, you can keep passing it around "forever" just to have your manipulation of the object disappear, because somewhere along the way you completely forgot this was a temporary." That is, in the following: #include <iostream> void modifyValue(int& rValue) { rValue++; } int main() { modifyValue(9899); return 0; } If an rvalue could bind to a non-const lvalue reference,

Iterating through an rvalue container

我的梦境 提交于 2019-12-22 11:06:13
问题 Is the following code causing undefined behavior? std::map<int, vector<int>> foo() { return ... } BOOST_FOREACH(const int& i, foo()[42]) { std::cout << i << std::endl; } If undefined, What is the good way to fix it? What if I use c++11 range-for loop instead of BOOST_FOREACH? 回答1: This is, unfortunately, most probably undefined behavior. The problem is that you have two levels here: std::map<...> is an r-value, its lifetime will be expanded until the end of the full-expression std::vector<int

Reference initialization in C++

筅森魡賤 提交于 2019-12-22 04:26:34
问题 Can anybody explain to me why there is a difference between these two statements? class A{}; const A& a = A(); // correct A& b = A(); // wrong It says invalid initialization of non-const reference of type A& from a temporary of type A Why does const matter here? 回答1: Non-const references must be initialised with l-values. If you could initialise them with temporaries, then what would the following do? int& foo = 5; foo = 6; // ?! const references have the special property that they extend the