A non-const reference may only be bound to an lvalue. This section presents an intentionally simplified definition of lvalues and rvalues. A non-const reference may only be bound to an lvalue

 
 This section presents an intentionally simplified definition of lvalues and rvaluesA non-const reference may only be bound to an lvalue A reference (of any kind) is just an alias for the referenced object

for example, to get a reference to the element. Testing tools for web developers. – Kerrek SB. Fun fact: /W3 is set. 2. Non-const references cannot bind to rvalues, it's as simple as that. In fact, if the function returns a &, const& or &&, the object must exist elsewhere with another identity in practice. You are returning a copy of A from test so *c triggers the construction of a copy of c. at member function does not return a reference to bool, but a proxy object that can be assigned to and converted to bool. However, you might need at that returns non-const reference too. You normally point to some spot in memory where you stored a value of interest. Without rvalue expression, we could do only one of the copy assignment/constructor and move assignment/constructor. 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. Let's look at std::vector for example: reference at( size_type pos ); const_reference at( size_type pos ) const; Would you look at that. @Nater The kind of reference (const/lvalue/rvalue) is irrelevant to the lifetime extension rules. void checkMe (shared_ptr<string>& param = shared_ptr<string> ()); This MSDN article says it is a /W4 warning. A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:I can't be bothered to go looking at that code, but. Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i. You are returning a copy of A from test so *c triggers the construction of a copy of c. The second const is good, as is stops the source item being modified. 3 -- Lvalue references ), we discussed how an lvalue reference can only bind to a modifiable lvalue. 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. See universal. 3. But result of such conversion is an rvalue, so your reference to non-const cannot be bound to it. ) Note that irr doesn't bind to iptr; so any modification on. 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. I have looked elsewhere on this site and read similar postings about this error: "initial value of reference to a non-const must be lvalue. ref]/5:. Follow edited Nov 15, 2016 at. Since rvalues cannot be bound to non-const lvalue references, this condition is not satisfied here. If t returns a local variable, then you get a dangling reference, since that variable is gone after the call. Or, passing it by const reference will also work, since a const lvalue reference can be. In fact, in terms of overload resolution, an rvalue prefers to be bound to an rvalue reference than to an lvalue const reference. 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). int & a=fun (); does not work because a is a non-const reference and fun () is an rvalue expression. (I) An rvalue had been bound to an lvalue reference to a non-const or volatile type. You can pass lvalues to functions taking rvalues as arguments (tested using a C++ editor). , cv1 shall be const), or the reference shall be an rvalue. col(0) is an rvalue, not an lvalue. Non-const reference may only be bound to an lvalue. 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. A const lvalue reference can be initialized from a bit-field. rvalue references are marked with two ampersands (&&). But if you are asking why this doesn't. Looks like an X-Y problem. And since that the converted initializer is an xvalue not prvalue, [conv. This constness can be cast away with a const_cast<>. 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. . View Site LeadersThe result is an lvalue if T is an lvalue reference type or an rvalue reference to function type (8. . 3. So if the function binds to a rvalue reference, what is seen at the end by the compiler for a certain type T is: std::is_rvalue_reference<T>::value. const foo&& can only bind to an rvalue, but const foo& can bind to both lvalues and rvalues. There is no such thing as a const rvalue, since an rvalue permits a "destructive read". Note that for const auto& foo, const is qualified on the auto part, i. obj in f is an lvalue expression, and will therefore be treated as such. So in your case, you need to rewrite your. ;, x is an lvalue denoting a float whose value is the result of converting a to double and back. I have fixed these issues and completely understand how/why it gives a warning. And an rvalue reference is a reference that binds to an rvalue. And until now we've only touched what already used to happen in C++98. 1. It's fairly obvious why int &ri3 = 2; (without the const) is invalid, but that doesn't imply that const int &ri3 = 2; is valid. "A reference to type 'cv1 T1' is initialized" refers to the variable that is being initialized, not to the expression in its initializer. First of all, an argument to such a reference must have static storage duration and linkage, which your variable cannot have both as it is defined in block-scope. Taking a constant reference to a temporary extends the life of that temporary to as long as the reference lives, allowing you to access any readable state. 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 C++ reference is similar to a pointer, but acts more like an alias. [2] Then, the resulting value is placed in a temporary variable of type T. inline B& operator<< (B&& b, int) {. The lifetime extension is not transitive through a. Undefined behavior can sometimes look like it's working. However, this is deceptive, because it may or may not be an rvalue reference depending on the type of T. The version with const Integer & works as const lvalue references can be bound to both lvalues and rvalues. an int literal) is not a lvalue, so int &p=255 fails. However, since Visual C++ allows this as an extension, how does it work? From what I've gathered, the standard does not allow this since you're getting a reference to a temporary variable, which can cause issues. Non-const reference may only be bound to an lvalue. Thus, the standard allows all types. I'm not sure why the compiler is trying to bind the non-const lvalue reference to an rvalue. long can be promoted to a long long, and then it gets bound to a const reference. Thus you know that you are allowed to manipulate it without damaging other data. If you need different semantics, you would require explicit specialization of template. lvalue reference 는 “data type. Just remove the Fraction(Fraction& f) constructor. In the second case, fun() returns a non-const lvalue reference, which can bind to another non-const reference, of course. 4 — Lvalue references to const. The simplest fix is to simply store the temporary object somewhere, first: Collider c=player. Share. 1 Answer. 3. One const and the other non-const. Because a reference to a non-const value can only bind to a modifiable lvalue (essentially a non-const variable), this means that pass by reference only works with arguments that are modifiable lvalues. And this is precisely what the compiler is telling you: The Lvalue refers to a modifiable object in c++ that can be either left or right side of the assignment operator. – Joseph Mansfield. key here is Key&& key - this is an lvalue! It has a name, and you can take its address. std::is_rvalue_reference<T&&>::value A temporary can only bind to a reference to a prvalue. You can normally hide the expression template type behind private members. There's no reason to make it a reference. This operator is trying to return an lvalue reference to a temporary created upon returning from the function: mat2& operator /= ( const GLfloat s. E may not have an anonymous union member. The call would bind to f(int&&). an lvalue, this constructor cannot be used, so the compiler is forced to use. error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int' return std::tie(a. But since it's a non-const reference, it cannot bind to an rvalue. However, an rvalue can be bound to a. They can bind to const lvalue-references because then a promise has been made. match. You must handle the case. 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. Share. g. struct Foo{}; { const auto & r = Foo{}; // Foo object not destroyed at semicolon. 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. An expression that designates a bit-field (e. @acannon828 Okay, but then you'd be modifying the pointer that is internal to World. for example, to get a reference to the element. This won't work. In 9. 5. 1. x, a. @KerrekSB: Binding a temporary to a const reference can cause a copy construction. However, lvalue references to const forbid any change to the object and thus you may bind them to an rvalue. 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. So how to solve that. " The C++ language doesn't allow you to bind an rvalue to a non-const reference because doing so would allow you to modify the rvalue - which would be impossible if it was a constant and undesirable if it was a temporary. Non-explicit constructors have their uses. Alex November 11, 2023 In the previous lesson ( 12. 1 1 1. It's just that type of that lvalue is "rvalue reference to Key ". The number of identifiers must equal the number of non-static data members. 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). 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 & A non-const reference may only be bound to an lvalue A non-const reference may only be bound to an lvalue. yet you can still change the data x by modifying x. In the following copy-initialization contexts, a move. If the initializer expression. a is an expression. 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. e. funcs], §13. @MichaelKrelin-hacker: Technically not, you cannot (ever) bind a reference to a value (or compile time constant), the standard is quite explicit as to what actually happens: Otherwise, a temporary of type “cv1 T1” is created and initialized from the initializer expression using the rules for a non-reference copy-initialization (8. initial value of reference to non-const must be an lvalue. If you want to capture the reference you need to declare a reference. When you pass a pointer by a non- const reference, you are telling the compiler that you are going to modify that. The page is trying to say that you can write m. 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. 1. The conversion produces an rvalue (i. A glvalue may be implicitly converted to a prvalue with lvalue-to-rvalue,. i have a player class in which i have a function to return a SDL_Rect for the dimensions and the position of my player object: SDL_Rect Player::pos () { return SDL_Rect { mPosX, mPosY, PLAYER_WIDTH, PLAYER_HEIGHT }; } i get the error: "initial value of. The second version is only allowed non-const rvalues because you can't implicitly strip const from the referencee and rvalue references don't allow lvalues to bind. The default is -qlanglvl. , cv1 shall be const), or the reference shall be an rvalue reference. 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. The binding rules for rvalue references now work differently in one aspect. 15. Non. A rvalue can be used as const T&, however, the compiler complains about binding a non-const lvalue to a rvalue. So if the function binds to a rvalue reference, what is seen at the end by the compiler for a certain type T is: std::is_rvalue_reference<T>::value. m. Actually the Standard say so: 8. Follow edited Apr 5, 2021 at 12:41. 68 initial value of reference to non-const must be an lvalue. & attr  (optional) declarator. Writing it gives you the chance to do it wrong (which you already did by. If P is a forwarding reference and the argument is an lvalue, the type “lvalue reference to A ” is used in place of A for type deduction. Community Bot. Can someone given an example of a "non-const lvalue reference"? I need to pass an object to a routine where the object's state will be modified, after the routine has completed I expect to use the object with the modified state. So if this is in the type Object:So we have a reference being initialized by an xvalue of type const foo. I don't get why the make_range function doesn't work unless I remove the View (View<C>& r) constructor. 1. 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. (5. The Python-side. 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. 3. 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. Reload to refresh your session. (コンパイラは VS2012) warning C4239: nonstandard extension used : 'initializing' : conversion from 'A' to 'A &' A non-const reference may only be bound to an lvalue. 2. (Binding to a const reference is allowed. 2. Actor & actor = get_actor_ref_from_ped (PLAYER::PLAYER_PED_ID ()); ^^^^^^^ reference. If you are unsure what an lvalue expression is, see this answer. i. Are there specific scenarios where binding temporary to non-const reference is allowed. lvalue references are marked with one ampersand (&). Your code has two problems. By the way, don’t return const values from a function, because you make it impossible to use move semantics. reference to type 'myclass' could not bind to an rvalue of type 'myclass *'. Allowing non-const references to bind to r-values leads to extremely confusing code. name. print(); This one matches the third constructor, and moves the value inside of the storage. std::vector<bool> is special from all other std::vector specializations. e. a. 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. 5. and not. 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. It cannot be done with lvalue references to non-const since they cannot be bound to rvalues. Without the function, you are essentially writing: int x = 10; // x is an l-value int &get_x = x; // just a variable instead of a function get_x = 20; // assignment is ok By float&, he means he wants to take a reference to a float. Case 3: binding to data members. Non-const lvalue reference to type 'Common::XYZCallbackInterface' cannot bind to a temporary of type 'Common::XYZCallbackInterface *'. When I discovered this, it seemed odd to me, so I tried. You obviously can't point to a temporary. The problem is that a non-const lvalue reference cannot bind to a temporary, which is an rvalue. . Share. Oct 10, 2013 at 22:07. Universal reference, or forwarding reference, only happen because of reference collapsing. Although the standard formulates it in other words (C++17 standard draft [dcl. 7 = a; The compiler / interpreter will work out the right hand side (which may or may not be const), and then put it into the left hand side. Find more info here. For some convenience, the const refs were "extended" to be able to point to a temporary. 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. For reference, the sentence that totally misled me is in [over. " followed by a specification of how the result of the conversion is determined. non-const reference of type from an rvalue. A non-const reference may only be bound to an lvalue? I am debugging MSDN code from, (VS. int x = 1000; const int &r = x; In this case, its a const reference to a non const variable. Regarding the second question. The compiler automatically generates a temporary that the reference is bound to. The foo () function accepts a non-const lvalue reference as an argument, which implies one can modify (read/write) the supplied parameter. The only time that lifetime is extended is when a prvalue (or an xvalue referring to a member of a prvalue) is bound to a reference variable, and the lifetime of the prvalue is extended to that of the variable:. This could also be achieved with a non-const lvalue reference, but then they would have to. it doesn't say anything else. You can correct the cases where the message is emitted so that your code is standard compliant. I dont know if its bug in compiler or is it intended. It can take rvalues because it is marked const and rvalues are allowed to bind to const lvalue references. reference to type 'myclass' could not bind to an rvalue of type 'myclass *'. Follow. if binding temporary to local non-const lvalue reference is allowed, you may write the code like this :. 12. 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. Expression like a+b will return some constant. operator[] is - either change the return type of the function from Value* to const Value&, or return *cleverconfig[name]; With the option -qinfo=por specified, when the compiler chooses such a binding, the following informational message is emitted. This is fulfilled by two types being similar, which basically means if they are the same type with the same number of pointers but possibly different cv-qualifiers (e. a. Add a comment. What is the reason behind disallowing binding an rvalue to an lvalue reference. That is special syntax for a so-called forwarding reference. g. Nov 15, 2016 at 14:14. You know, just like any other use of const. e. Confusion between rvalue references and const lvalue references as parameter. rvalues can only be bound to const lvalue references. I recently filed a bug against MSVC which relates to this, where the non-standard behavior caused standard-compliant code to fail to compile and/or compile with a deviant behavior. Some compilers allow int &r = 5; as an extension, so it makes sense, it's just not allowed by the standard. There are two overloads. (The small difference is that the the lambda-expression is converted to a temporary std::function - but that still can't be bound to a non-const reference). h(418) : warning C4239: nonstandard extension used : 'argument' : conversion from 'XUTIL::xList<T>::iterator' to. Since the temporary B that's returned by source () is not. Now it makes actually sense to take its address, as it is an lvalue for all intents and purposes. and another 7 more if your interested, all saying about the same thing. The behaviour of this is to copy-initialize a temporary of the same type as the reference. 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. "non-const lvalue reference to type 'QByteArray' cannot bind to a temporary of type 'QByteArray'". e. Const reference can be bounded to. The problem is that a non-const lvalue reference cannot bind to a temporary, which is an rvalue. e. For lvalue-references (that is, the type T&) there isn't. I have looked elsewhere on this site and read similar postings about this error: "initial value of reference to a non-const must be lvalue. 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. 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). Generally speaking, when a function takes a parameter by non-const. 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. In the second case, fun() returns a non-const lvalue reference, which can bind to another non-const reference, of course. rvalue Reference Cannot Bind to a Named lvalue. , temporary) double but a temporary cannot be bound to a non-const reference. To declare an lvalue reference type, we use an ampersand (&) in the type declaration: int // a normal int type int& // an lvalue reference to an int object double& //. Assume a variable name as a label attached to its location in memory. What you're trying to perform is making a reference to a temporary value which is not allowed. Because a reference to a non-const value can only bind to a modifiable lvalue (essentially a non. 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. Value categories are applied to expressions, not objects. However, int can be implicitly converted to double and this is happening. There are better ways to solve your problems. This can only bind to a const reference, and then the objec's lifetime will be extended to the lifetime of the const reference it is bound to (hence "binding"). Why can't I bind an Rvalue to a non-const Lvalue reference? C++ does not allow binding Rvalues to non-const Lvalue references because Lvalue references can modify the object they are bound to, and Rvalues. g. However, A can be converted to an lvalue of type int, and const int is reference-compatible with int, so reference x of type const int can be bound to the conversion result of A(). Sorted by: 6. If C++ allowed you to take literals by non-const reference, then it would either: Have to allow literals to change their meaning dynamically, allowing you to make 1 become 2. Const reference can be bounded to. If you want to check if it returns a non-const reference, you need to check that, not whether you can assign to it. 1. The number of identifiers must equal the number of non-static data members. ) But there is no way to show me how to solve it;You may modify a non-const object through a non-const reference. Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i. It can take rvalues because it is marked const and rvalues are allowed to bind to const lvalue references. We can take the address of an lvalue, but not of an rvalue. Thank you for answering. e. My guess is that this restriction has historical roots in the C++98 standard where rvalues were limited to temporaries, that were fully managed by the compiler. Note that the table indicates that an rvalue cannot bind to a non-const lvalue reference. having an address). doesn't that mean that an rvalue ref is an lvalue. { A res; res. Note that there is one exception: there can be lvalue const reference binding to an rvalue. Actually for simple types you should prefer to pass by value instead, and let the optimizer worry about providing the best implementation. The compiler preventing this is a way of catching these kinds of errors. RVO may explain this particular quark, but you cannot return a reference to something that doesn't exist. thanks in advance, George. 0f, c); The other similar calls need to be fixed too. thanks in advance, George For lvalue references, T is deduced to be an lvalue reference, and for rvalue references, T is deduced to be a non-reference. The warning tells you your code now behaves differently than in earlier versions of Visual C++. Both const and non-const reference can be binded to a lvalue. However, int can be implicitly converted to double and this is happening. The conversion produces an rvalue (i. A temporary can only bind to const lvalue references, or rvalue references. A non-const reference must be bound to lvalue (i. 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. My guess is that this restriction has historical roots in the C++98 standard where rvalues were limited to temporaries, that were fully managed by the compiler. GetCollider (). The core of your question is: can rvalues be bound to non-const lvalue references?. the expression c is an lvalue, even though the reference may have been bound to a temporary object at the time of calling. Change the declaration of the function GetStart like: Point & GetStart(); Also at least the function GetEnd should be changed like: Point & GetEnd(); You could overload the functions for constant and non-constant objects: You may say, "ha, that's allowed because we want to provide the programmer with some flexibility to do "stupid" things that are in fact not that stupid", which is the reason I can hardly buy because if I buy this excuse, I think that "binding temporary to non-const lvalue reference" can be justified using the same reason. 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. 25th May 2022, 8:44 AM. const int & is a const lvalue reference. If U is t’s underlying non-reference type (namely std::remove_reference_t<decltype(t)>), then T. then the reference is bound to the initializer expression lvalue. g. e. (non const) lvalue reference and rvalue that also means that you can convert the rvalue into an lvalue and therefore. aspx. C++: Variable that is passed by const referance changes value. 2/5 in N4140): A temporary bound to a reference parameter in a function call (5. An expression that designates a bit-field (e. The compiler automatically generates a temporary that the reference is bound to. Thus, in case of your variable b: T = int ==> T&& becomes int&& T = int& ==> T&& becomes int. qual] or even [conv. A non-const reference may only be bound to an lvalue? (too old to reply) George 15 years ago Hello everyone, I am debugging MSDN code from,. In function 'int main()': Line 15: error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string' compilation terminated due to -Wfatal-errors. The Rvalue refers to a value stored at an address in the memory. 4. the first version essentially returns second of said pair directly. The reference returned from get_value is bound to x which is an l-value, and that's allowed. I have to think for a while-_-!. Reference-compatibility allows extra cv-qualifications in the reference type. The problem is that auto and decltype side-step the whole public/private thing, allowing you to create types that you. reference (such as the B& parameter in the B::B (B&) constructor) can only. Unfortunately, they may compile with one common compiler, due to language. , cv1 shall be const), or the reference shall be an rvalue reference. For non-static member functions, the type of the implicit object parameter is — “lvalue reference to cv X” for functions declared without a ref-qualifier or with the & ref-qualifier — “rvalue reference to cv X” for functions declared with the && ref. E may not have an anonymous union member. int f( int ); int f( int && ); int f( int const & ); int q = f( 3 ); Removing f( int ) causes both Clang and GCC to prefer the rvalue reference over the lvalue reference. 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. Technically, auto is the root of the problem. Cannot bind non-const lvalue reference to an rvalue. int const&x = 42; // It's ok. It isn't "hard to spell type"; the compiler will prevent you from using the type explicitly. temporary] ( §12. 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 expected 12. 5. And the this pointer is a const pointer, so the instance cannot be changed. e. Share. Calling operator + second time won't be possible because a temporary object can not be passed as reference to a non-const-qualified object. end()) is a temporary object and cannot be bound to lvalue reference. g. warning C4239: nonstandard extension used: 'default argument': conversion from 'std::shared_ptr' to 'std::shared_ptr &'. Even Microsoft engineers like u/STL recommend avoiding this "extension" if I recall correctly. It's unclear what you mean by "has". 3 -- Lvalue references ), we discussed how an lvalue reference can only bind to a modifiable lvalue. Lesley Lai has a blog post on this: “The implication. You can't. 6. Since there are some non-modifiable lvalues (so we do not always need to modify values through its reference). 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. And plus more, in this case if I called. There are exceptions, however. void foo(int& x)) and then complaining that you can't call foo(5). 12. Suppose r is an rvalue reference or nonvolatile const lvalue reference to type T, and r is to be initialized by an expression e of type U. obj & a1 = bar(); invalid initialization of non-const reference of type ‘obj&’ from an rvalue of type ‘obj’ using g++.