RVO may explain this particular quark, but you cannot return a reference to something that doesn't exist. Assignment to references, on the other hand, is implicit, so if a is of type int& you simply need to write a=b to make a a reference to b. Example 5 @relent95 Yes, whether the id-expression refers to a variable of reference or non-reference type doesn't matter because of what you quoted. std::is_rvalue_reference<T&&>::value A temporary can only bind to a reference to a prvalue. It's the specific case where changing T& to const T& does more than just ban modifications. 3. The this pointer is defined to be a prvalue, and your function takes an lvalue. Hot Network Questions Identifying traffic signals for colour blind peopleBut thinking further about it, I think it might be OK :-) Imagine there were three consts (not just two) in const Array &operator=( const Array & ) const; The last const is unacceptable, as it can't even modify itself. However, lvalue references to const forbid any change to the object and thus you may bind them to an rvalue. e. What getPtr () return: std::shared_ptr<int> getPtr (int val) { } is an rvalue reference. Thank you. For example, a const lvalue reference should bind to both lvalue and rvalue arguments, and a non-const lvalue reference should bind to a non-const lvalue, but refuse to bind to rvalues and const lvalues. e. rvalue Reference Cannot Bind to a Named lvalue. If t returns by rvalue reference, you obtain a reference to whatever was returned. Actually the Standard say so: 8. The second const is good, as is stops the source item being modified. rvalues can only be bound to const lvalue references. There are two overloads. The first variant returns a reference to the actual value associated with the key test, whereas the second one returns a reference to the map element, which is a pair<const key_type, mapped_type>, i. When the first element of the pair is declared as const, you can't bind a non-const rvalue reference (std::string&&) to it. It can appear only on the right-hand side of the assignment operator. E may not have an anonymous union member. This operator is trying to return an lvalue reference to a temporary created upon returning from the function: mat2& operator /= ( const GLfloat s. 2) x is a variable of non-reference type that is usable in constant expressions and has no mutable subobjects, and E is an element of the set of potential results of an expression of non-volatile-qualified non-class type to which the lvalue-to-rvalue conversion is applied, or. It looks like well formed code with defined behavior to me. The C++ standard does not allow the binding of an anonymous temporary to a reference, although some compilers allow it as an extension. 2. an lvalue, this constructor cannot be used, so the compiler is forced to use. With /W4 you'd see this: warning C4239: nonstandard extension used : 'initializing' : conversion from 'Foo' to 'Foo &' 1> A non-const reference may only be bound to an lvalue Specifically, MSVC 2013 will give a warning of "mysourcefile. If you are trying to modify the variable 'pImage' inside the method 'GetImage ()' you should either be passing a pointer or a reference to it (not doing both). The forward should decay into an lvalue reference anyways, right? c++; perfect-forwarding; Share. There's a special rule in C++ template deduction rules which says that when the parameter type is T&& where T is a deduced type, and the argument is an lvalue of type. But a more proper fix is to change the parameter to a const. This sample shows the Microsoft extension that allows a temporary of a user-defined type to be bound to a non-const lvalue reference. Lvalue and rvalue expressions. e. If you want to work with rvalues, perhaps use an rvalue reference. If you compile with the /Wall flag, you will be given the answer by the compiler itself:. e. Your code has two problems. Consider also this: the language has no way of knowing that the lvalue reference returned by the iterator's operator * or the vector's operator[] refers to something whose lifetime is bound to that of. Returning non-const lvalue reference. 12. v; return res; } You should make the member function a const member function too since it does not modify the object. This constness can be cast away with a const_cast<>. Consider another last example: const int&& r2 = static_cast<int&&>(0); The same wording as above applies: The initializer expression is an rvalue (xvalue) and cv1 T1 (const int) is reference-compatible with cv2 T2 (int). An rvalue may be used to initialize a const lvalue [ rvalue] reference, in which case the lifetime of the object identified by the rvalue is extended until the scope of the reference ends. temporary] ( §12. Notes: A non-const or volatile lvalue reference cannot be bound to anrvalue of a built-in type. The lifetime extension is not transitive through a. Fibonacci Series in C++. Sometimes even for the original developer, but definitely for future maintainers. All rvalues are non-const. g. Apr 13, 2017 at 13:00. 3. ; T is not reference-related to U. it is explained that an lvalue is when you can take its address. Am getting cannot bind non-const lvalue reference of type ‘Type&’ to an rvalue of type 'Type'The function returns a pointer, which you are trying to bind to a reference. This seems to be well defined however (writing to a temporary value is just like writing to any value, the lifetime has no relevancy to the. Pointers have a difference, pointer can be changed. It is a name of a reference, and references refer to objects. @Nater The kind of reference (const/lvalue/rvalue) is irrelevant to the lifetime extension rules. (After all, there is no actual long long to refer to. Fibonacci Series in C++. C++ does not give that feature to non-const references: A function lvalue; If an rvalue reference or a non-volatile const lvalue reference r to type T is to be initialized by the expression e, and T is reference-compatible with U, reference r can be initialized by expression e and bound directly to e or a base class subobject of e unless T is an inaccessible or ambiguous base class of U. If you need different semantics, you would require explicit specialization of template. @acannon828 Okay, but then you'd be modifying the pointer that is internal to World. Otherwise, the reference you get behaves more. initial value of reference to non-const must be an lvalue, Passing an object type by. The int* needs to be converted to void* firstly, which is a temporary object and could be bound to rvalue-reference. Sometimes even for the original developer, but definitely for future maintainers. Const reference to temporary object does not extend its lifetime. In fact, if the function returns a &, const& or &&, the object must exist elsewhere with another identity in practice. Non-const reference may only be bound to an lvalue. In summary, after const float & x = true ? a : 2. Thus you know that you are allowed to manipulate it without damaging other data. What you want is in 40two's answer, but make sure to forward the parameter t. If t returns by lvalue reference, the code does not compile because a rvalue reference cannot bind to it. However sometimes it is desired to ensure that you can only pass lvalues to a function (this is the case for std::ref for one). The Rvalue refers to a value stored at an address in the memory. That's not it. ). A modifiable lvalue is any lvalue expression of complete, non-array type which is not const-qualified, and, if it's a struct/union, has no members that are const-qualified, recursively. Once it is bound, it's just a reference. a. It's not against the rules in C++ to use a non-const reference but I think it lends to massive confusion and potential bugs. Your conclusion happens to be correct, but it doesn't follow from your premise. But the principle is the same. A temporary can only bind to const lvalue references, or rvalue references. Just like how we don't want the first example to create a temporary int object (a copy of x) and then bind r to that, in the. Solution 3: When you call with , the address-of operator creates a temporary value , and you can't normally have references to temporary values because they are, well, temporary. You can call a non-const member function on a temporary because this does not involve binding of a reference. My understanding is that this is largely to avoid breaking several enormous legacy codebases that rely on this "extension. It's not against the rules in C++ to use a non-const reference but I think it lends to massive confusion and potential bugs. A non-const reference can be used to change the value of the variable it is referring to. Only modifiable lvalue expressions may be used as arguments to increment/decrement, and as left-hand arguments of assignment and compound. You can normally hide the expression template type behind private members. Because as_const doesn't take the argument as const reference. v = this->v*a. The compiler will generate it for you. If non-const lvalue references were allowed to refer to rvalues, you would never know if the object referred to was. The linked page uses the words "rvalue" and "lvalue" incorrectly . col(0) = whatever; to write to the column. 4) const lvalues can be passed to the parameter. However, int can be implicitly converted to double and this is happening. So long as the reference is initially bound to an l-value, everything is fine (so long as you don't use a reference to a stack local variable, of course). the first version essentially returns second of said pair directly. It's not against the rules in C++ to use a non-const reference but I think it lends to massive confusion and potential bugs. Follow edited Apr 5, 2021 at 12:41. T and U) are never reference types. A non-const reference may only be bound to an lvalue[/quote] 1 Reply Last reply Reply Quote 0. If you are asking why this code doesn't work : const string& val = "hello" string& val = "hello" the answer is you are trying to redeclare the same variable (val) with conflicting definition. A reference is supposed to work a lot like a pointer in a sense. There's no difference between a bound rvalue reference and a bound lvalue reference. 12. 2. c++; Share. If I were to call it with an rvalue, C++ would shout at me. Unless an object is created in the read-only section of a program, it is open for modifiction without adverse consequences. bind to an lvalue. Non-const reference may only be bound to an lvalue. a nonconst reference could only binded to lvalue. Rvalue references should be unconditionally cast to rvalues when forwarding them to other functions: void sink (ConcreteType&& ct) // can only be called on rvalues { collection. Value categories pertain to expressions, not objects. r can be bound to the conversion result of e or a base class of e if the following conditions are satisfied. g. This allows you to explicitly move from an lvalue, using move. Writing it gives you the chance to do it wrong (which you already did by. 5). rvalue reference versus non-const lvalue. Non. Both const and non-const reference can be binded to a lvalue. The whole idea of forwarding is to accept any value category and preserve it for future calls. Visual C++ is non-compliant with the standard in allowing binding of temporaries to non-const lvalue references. m. Since C++11, two kinds of references have existed - lvalue and rvalue references. For example, the argument might be a reference to a node of a linked list, and within the function you may want to traverse the list, so you will want to be doing node = * (node. I do not quite understand why there is a warning A non-const reference may only be bound to an lvalue and 'B::B (A)' called instead of 'B::B (B &)'? think. A non-const lvalue reference can only bind to non-const lvalues. reference (such as the B& parameter in the B::B (B&) constructor) can only. Share. In the second case, fun () returns a non-const lvalue reference, which can bind to another non-const reference, of course. You would only need to create such a wrapper if you needed to do things with it that you can't do with C++ objects, such as storing it in an NSArray or. – Vlad from Moscow. You switched accounts on another tab or window. We can't bind rvalue reference to an lvalue also. init. end()) is a temporary object and cannot be bound to lvalue reference. Yes, some times it is very convenient to be able to locally modify a pass-by-value argument to a function. If encodeData() does not change dataBuff then the simplest solution is to take a const & which can bind to a temproary. – n. This way, if the user passes in a U as an lvalue, it will be passed as U&, and if the user passes in a U as an rvalue, it will be passed as U&&. unsigned int&). That is to say, usage of a reference is syntactically identical to usage of the referent. the expression c is an lvalue, even though the reference may have been bound to a temporary object at the time of calling. In other words, in your first example the types actually do match. If U is t’s underlying non-reference type (namely std::remove_reference_t<decltype(t)>), then T. The compiler preventing this is a way of catching these kinds of errors. A function lvalue; If an rvalue reference or a non-volatile const lvalue reference r to type T is to be initialized by the expression e, and T is reference-compatible with U, reference r can be initialized by expression e and bound directly toe or a base class subobject of e unless T is an inaccessible or ambiguous base class of U. By the way, don’t return const values from a function, because you make it impossible to use move semantics. e. C++. In this case, returning a non-const lvalue reference compiles because x is an lvalue (just one whose lifetime is about to end). std::string&& rref = std::string("hello"); rref has value category lvalue, and it designates a temporary object. The number of identifiers must equal the number of non-static data members. A C++ reference is similar to a pointer, but acts more like an alias. However, int can be implicitly converted to double and this is happening. Here you are taking a reference to a uint8Vect_t. The option -qlanglvl=compatrvaluebinding instructs the compiler to allow a non-const or volatile lvalue reference to bind to an. then the reference is bound to the initializer expression lvalue. – You may not bind a temporary object with a non-constant lvalue reference. @YueZhou Function lvalues may be bound to rvalue references. I agree with the commenter 康桓瑋 that remove_rvalue_reference is a good name for this. obj in f is an lvalue expression, and will therefore be treated as such. (1) && attr (optional) declarator. static_cast<typename remove_reference<T>::type&&> (t) The result of the function call is an rvalue (specifically, an xvalue ), so it can be bound to an rvalue reference where the function argument couldn't. (5. 2. This extends the lifetime of the temporary: base * const &rp = (base*)p; Or bind the reference to an lvalue: base * b = p; base * &rp = b; Share. In such cases: [1] First, implicit type conversion to T is applied if necessary. A non-const reference may only be bound to an lvalue? I am debugging MSDN code from, (VS. Constructor by the definition does not have a return value. Sounds like you actually want getPlayer to return a reference too and then to. Saturday, December 15, 2007 4:49 AM. 1. May 4, 2013 at 16:38. This rule covers not only cases such as. Sometimes even for the original developer, but definitely for future maintainers. Rvalues (including xvalues) can be bound to const lvalue references so that you can pass a temporary to a function with such a parameter:With pointers, you can mostly correctly use const and non const versions, whatever is more appropriate (i. Since the temporary B that's returned by source () is not. By default, or if /Zc:referenceBinding- is specified, the compiler allows such expressions as a Microsoft extension, but a level 4 warning is issued. 3 Answers. A const lvalue reference can be initialized from a bit-field. Jun 17, 2016 at 3:16. This won't work. Not that std::forward has a return type that looks like T&&. This extends the lifetime of the temporary: base * const &rp = (base*)p; Or bind the reference to an lvalue: base * b = p; base * &rp = b; Share. See universal. However, when you use a const reference to a non-const object, you are asking the compiler to not let you modify the object through that particular. Assume a variable name as a label attached to its location in memory. – Joseph Mansfield. So how to solve that. A const reference prolongs a lifetime of a temporary object bound to it, so it is destroyed only when the reference goes out of scope. Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i. void my_function (const MyType & arg); This avoids the copy of these parameters in situations where they don’t need to be copied. A simple solution is: void foo (MyObject obj) { globalVec. 3. It allows you to do something like swap(a, b), and it will actually swap the values of a and b, instead of having to do swap. Non-const reference may only be bound to an lvalue. I don't get why the make_range function doesn't work unless I remove the View (View<C>& r) constructor. Unlike a reference to non-const (which can only bind to modifiable lvalues), a reference to. Const reference can be bounded to. h(418) : warning C4239: nonstandard extension used : 'argument' : conversion from 'XUTIL::xList<T>::iterator' to. and not. To be standards compliant, you need. C4239: nonstandard extension used : 'default argument' : conversion from 'QMap<QString,QVariant>' to 'QVariantMap &' A non-const reference may only be bound to an lvalue. A function parameter such as T&& t is known as a forwarding reference. Non-const reference may only be bound to an lvalue. MS Visual Studio compilers have allowed binding of non- const references to temporary objects but it is not sanctioned by the standard. I believe the relevant Standard paragraph is 8. an lvalue, this constructor cannot be used, so the compiler is forced to use. Apparently, the Standard agrees. The pre-C++ origin of the terms "lvalue" and "rvalue" might be related to "left" and "right" side of assignment, but that meaning is only applicable in a small subset of the C++ language. And const is a constraint imposed by the compiler to the variable that is declared as const. And an rvalue reference is a reference that binds to an rvalue. 3 The initialization of non-const reference. In your code, int & is a non-const lvalue reference. There are several (very constrained) circumstances in which the compiler, with language extensions enabled, will still allow a non-const lvalue reference to bind to an rvalue expression. What I have seen however is that you can bind an rvalue to an rvalue reference and since a named rvalue reference is inherently an lvalue, you can bind it to an lvalue reference. e. Calling a non-static member function of class X on an object that is not of type X, or of a type derived from X invokes undefined behavior. . If an rvalue is passed to factory, then an rvalue will be passed to T's constructor with the help of the forward function. So, when you call 'handle_ack_message ()' from this function, you're trying to pass an 'lvalue' to a function that only accepts an 'rvalue'. The type of such a reference must be a const qualified lvalue reference or a rvalue references. Constant lvalue references can be bound to all types of values, including non-constant lvalues, constant lvalues. By float&, he means he wants to take a reference to a float. Remember that an rvalue binds to a const lvalue reference, hence if you did: template <typename T> void foo (const T& bar) { /*. So the first fix is to not use the wrong technique here, and accept by an lvalue reference instead:The simple answer is that you are right in essence. . Declaring operator + to accept non-const references does not make. There is no such thing as a const rvalue, since an rvalue permits a "destructive read". Mark Forums Read; Quick Links. m. However, since a reference acts identically to the object being referenced, when using pass by reference, any changes made to the reference parameter will affect the argument: #include <iostream. A const lvalue reference or rvalue reference can be. { A res; res. rvalues are defined by exclusion, by saying that every expression is. Since you cannot access the result of that side-effect if you are passing a temporary, then I would conclude that you're very likely doing something wrong. 19 tricky. const reference to non-const object. The type of such a reference must be a const qualified lvalue reference or a rvalue references. 2 Answers. funcs], §13. long can be promoted to a long long, and then it gets bound to a const reference. And until now we've only touched what already used to happen in C++98. The standard has a concept of two types being reference-related. reference (such as the B& parameter in the B::B (B&) constructor) can only. Troubles understanding const in c++ (cannot bind non-const lvalue reference) 0. The rules about reference binding are that a non-const lvalue reference may only bind to an lvalue expression. CheckCollision(0. This program outputs: value = 5 value = 5. rvalue references are marked with two ampersands (&&). By using the const keyword when declaring an lvalue reference, we tell an lvalue reference to treat the object it is referential when const. Hence, C++ does not permit a non-const reference to a const variable. an identifier) that resolves to a non-type non-static member of X or of a base class of X, is transformed to a member access. Lvalue reference to const. –The pointer returned by the function cannot be bound to a reference. 1. You can't bind a temporary to a non-const lvalue-reference because it doesn't make much sense to modify, say, a literal like 42. [3] Finally, this temporary variable is used as the value of the initializer. C++/SDL "initial value of reference to a non-const must be an lvalue". But result of such conversion is an rvalue, so your reference to non-const cannot be bound to it. Ok, so, I already know that returning a local variable as reference will cause undefined behavior when we try to use it and that we can create a non-const reference to only form a lvalue variable. 0 Invalid initialization of non-const reference from a. The number of identifiers must equal the number of non-static data members. You can correct the cases where the message is emitted so that your code is standard compliant. — Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i. As I understand it, the compiler has to create an implicit read-only object so that ri3 can be a reference to it; note that &ri3 yields a valid address. reference to type 'myclass' could not bind to an rvalue of type 'myclass *'. const int x = 0; int&& r = x; Here, we don't have an exact match in types: the reference wants to bind to an int, but the initializer expression has type const int. However, you might need at that returns non-const reference too. Rule: lvalue, rvalue, const or non-const objects can bind to const lvalue parameters. Passing by reference, by a const reference wouldn't cost more than passing by value, especially for templates. “An old special-case permits an rvalue to be bound to an lvalue reference to non-const type when that reference is the. You signed out in another tab or window. On the other hand lvalue references to const forbids any change to the object they reference and thus you may bind them to a rvalue. Non-const reference may only be bound to an lvalue. C++ prohibits passing a temporary object as a non-const reference parameter. I do not quite understand why there is a warning A non-const reference may only be bound to an lvalue? A const reference can be bound to: R-value L-value A non-const reference can be bound to: L-value This means that you can do this: int const &x = 5; But you _can't_ do this: int &x = 5;, thus preventing you from trying to modify a literal, or. cpp struct S { }; void f(S&) { } S g() { return S {}; } int main() { S& s = g (); // warning C4239 at /W4 const S& cs = g (); // okay, bound to const ref f (g ()); // Extension: error. Create_moneys () is a function that takes a mutable reference to a pointer. 3 Answers. Use a const reference, which can be bound to rvalues. In C++03 the only reason to use the const& trick is in the case where. (Case 1 in the below program). If you are unsure what an lvalue expression is, see this answer. Notably, types of expressions (i. If you want to capture the reference you need to declare a reference. You can change the parameter type to const char* in or const char* const & in if in won't be modified in UTF8toWide() , or use a named variable instead. Furthermore, we don't know if somefunc2 modifies the referenced byte, and if it does then we don't know what should happen to the other byte. 3. If you want to check if it returns a non-const reference, you need to check that, not whether you can assign to it. cannot bind non-const lvalue reference of type to an rvalue of type 0 Implementation of the decorator class in C++ using a member reference to the decorated object not working as expected12. Only local const references prolong the lifespan. There are exceptions, however. Actor actor = get_actor_ref_from_ped (PLAYER::PLAYER_PED_ID ()); Is going to make a copy of the value returned from the function as it calls the copy constructor. 806 3 3 gold badges 12 12 silver badges 20 20 bronze badges. " followed by a specification of how the result of the conversion is determined. Note also that if you simply use CList<DATA>, the second template argument ARG_TYPE is correctly deduced to be const DATA& by default, as per CList template declaration (TYPE = DATA, ARG_TYPE = const DATA&): template<class TYPE, class ARG_TYPE = const TYPE&> class CList : public CObjectT& data; There's your problem. There's no reason to make it a reference. 4. Anything that is capable of returning a constant expression or value. print(); This one matches the third constructor, and moves the value inside of the storage. So obviously it's not portable. 0; // error: not an lvalue and reference not const int i = 2; double& rd3 = i; // error: type mismatch and reference not const —end example]A non-const reference may only be bound to an lvalue[/quote] 1 Reply Last reply Reply Quote 0. For some convenience, the const refs were "extended" to be able to point to a temporary. Would you explain why you need a non-const reference that cannot bind to non-const objects?. That's an exception to the general rule that it is impossible for lvalues to be bound to rvalue. Explanation: const lvalue indicates that the callee wants a read-only view of the object and it does not matter what type of object the caller pass as the argument. Follow edited Oct 5 at. Thus, the standard allows all types. Unlike a reference to non-const (which can only bind to modifiable lvalues), a reference to const can bind to modifiable lvalues, non-modifiable lvalues, and rvalues. If you used a reference to const, it would extend the lifetime of the temporary result of the implicit conversion: const int * const &j = i;The iterator object itself refers to an element of the container. The rest of the article will elaborate on this definition. This approach does not work for two reasons: First, because we modify the source object, we have to pass it as a non-const reference. Non-const reference may only be bound to an lvalue. initial value of reference to non-const must be an lvalue when calling a function. x where s is an object of type struct S { int x:3; };) is an lvalue expression: it may be used on the left hand side of the assignment operator, but its address cannot be taken and a non-const lvalue reference cannot be bound to it. test (const std::string& a): a is const lvalue reference and like before I have lvalue and rvalue. It's the first const that I'm unsure of. An lvalue reference (commonly just called a reference since prior to C++11 there was only one type of reference) acts as an alias for an existing lvalue (such as a variable). Sorted by: 6. So an expression returning a non-const reference is still considered an lvalue. 3 -- Lvalue references ), we discussed how an lvalue reference can only bind to a modifiable lvalue. The page is trying to say that you can write m. 5. is an xvalue, class prvalue, array prvalue or function lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or. 2) persists until the completion of the full-expression containing the call. You have two options, depending on your intention. double && does not work for lvalues. 21. ReferencesAnother option is to make push() be a template with a forwarding reference of type U, using a concept/SFINAE to make sure that U is compatible with the class's main T type. g. Nov 15, 2016 at 14:14. const A& x = 1; //compile x = 2; //error! A&& xxx = 1; //compile A& xx = 1; //does not compile. its address could be got). 68 initial value of reference to non-const must be an lvalue. The simplest fix is to simply store the temporary object somewhere, first: Collider c=player. Calling operator + second time won't be possible because a temporary object can not be passed as reference to a non-const-qualified object. ctor] A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are. Similar rationale is applied to the const qualifier. struct S {}; f<S {}> (); // ok. C++ : Non-const reference may only be bound to an lvalueTo Access My Live Chat Page, On Google, Search for "hows tech developer connect"As promised, I have a. bind to an lvalue. For reference, the sentence that totally misled me is in [over. const unsigned int&), (and its lifetime is extended to the lifetime of the reference,) but can't be bound to lvalue-reference to non-const (i. Follow edited Nov 15, 2016 at. References to non-pointer values make more sense. In the example above, SomeClass() is not bound to an identifier, so it is an rvalue and can be bound to an rvalue reference -- but not an lvalue reference. 3. An rvalue reference can only bind to non-const rvalues. Non-const references cannot bind to rvalues, it's as simple as that. push() can use an if constexpr. (2) (since C++11) 1) Lvalue reference declarator: the declaration S& D; declares D as an lvalue reference to the type determined by decl-specifier-seq S. So the parameter list for a copy constructor consists of an const lvalue reference, like const B& x . r-value:-. Note that for const auto& foo, const is qualified on the auto part, i. Non-const reference may only be bound to an lvalue. 1. Now it makes actually sense to take its address, as it is an lvalue for all intents and purposes. (PS the lifetime of the temporary is extended to the lifetime of the reference. " Rule 2, "A non-const reference shall not be bount to a bit-field". Basically, VS will allocate the space somewhere and just let the reference point to it, as if it was a reference-to- const without the constness (or in C++11 an rvalue reference). However, I am. There are two overloads. I am still studying what is the reason in essence in compiler why a non-const reference can not be binded to a rvalue. Lvalue references to const can be bound to. This rule does not reflect some underlying. 4 — Lvalue references to const. In the above code, getStr(); on line 12 is an rvalue since it returns a temporary object as well as the right-hand side of the expression on line 13. a copy would be needed). push_back (std::move (obj)); } If caller passes an lvalue, then there is a copy (into the parameter) and a move (into the vector).