int &a = 5; // error: lvalue cannot be bound to rvalue 5 However, we can bind an rvalue to a const lvalue reference (const reference): const int &a = 5; // Valid In this case, the compiler. nik7. if binding temporary to local non-const lvalue reference is allowed, you may write the code like this :. For example, when passing things by value, or else with things like A a; B b = a;. A non-const reference must be bound to lvalue (i. and not. (2023/4/18 現在) 理由は引数の型が non-const reference で. h(418) : warning C4239: nonstandard extension used : 'argument' : conversion from 'XUTIL::xList<T>::iterator' to. After some investigation and help from the community, here is the answer:. 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. The best option is to return by copy. MS Visual Studio compilers have allowed binding of non- const references to temporary objects but it is not sanctioned by the standard. The binding rules for rvalue references now work differently in one aspect. Mark Forums Read; Quick Links. Other situations call for other needs, but today we will focus on constant references. e. The non-const subscript operator returns a non-const reference, which is your way of telling your callers (and the compiler) that your callers are allowed to modify the Fred object. v; return res; } You should make the member function a const member function too since it does not modify the object. And the this pointer is a const pointer, so the instance cannot be changed. Second, our new version of the copy constructor will just as happily transplant the internals of lvalues: IntVector v1; IntVector v2 (v1); // v1 is no longer. 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 to them. Changing it to void display (const double& arg) works because everything works the same as explained above. Same thing can be done with lvalue references to const: const int& x = 10. To reduce template instantiation overhead, I would recommend a more direct implementation:will result in output: Constructor called 42. 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. int* and void* are different types; you can't bind a int* to reference to void* directly. What "r-value reference for this` does is allow you to add another alternative: void RValueFunc () &&; This allows you to have a function that can only be called if the user calls it through a proper r-value. Because as_const doesn't take the argument as const reference. And since that the converted initializer is an xvalue not prvalue, [conv. Lvalue references to const can be bound to. The literal 0 is still a poor choice for its default value, considering that 0 is an int, and your type is. – The outcome is that the code compiles and works when using MSVC, but doesnt on GCC and Clang, with respective errors: GCC: cannot bind non-const lvalue reference of type 'FuncPtr<bool ()>&' to an rvalue of type 'FuncPtr<bool ()>' Clang: no matching constructor for initialization of 'A'. Secondly, your variable is const (as it is constexpr), and a non-const reference cannot be bound to a const object. 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. It work that way:. Jan 8, 2015 at 8:51. the expression c is an lvalue, even though the reference may have been bound to a temporary object at the time of calling. So in your case, you need to rewrite your. 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. Assuming standard data sizes, you have a reference to 2 bytes of data that you're trying to pass into a function that takes a reference to only 1 byte. Non-compliant compilers might allow a non-const or volatile lvalue reference to be bound to an rvalue. On the contrary, rvalues can be bound to const lvalue references. 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. //. double && does not work for lvalues. An entity (such as an object or function) that has. 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. 5The Lvalue refers to a modifiable object in c++ that can be either left or right side of the assignment operator. 5. However, now you've got a temporary A, and that cannot bind to a, which is a non-const lvalue reference. constexpr T& value() &; constexpr const T & value() const &; constexpr T&& value() &&; constexpr const T&& value() const &&; What is the point of returning a const rvalue reference? The only reason I can think of is to enable the compiler to help catch undefined behavior in (really really weird) cases like the followingA non-const reference may only be bound to an lvalue[/quote] Reply Quote 0. 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. If caller passes an rvalue, then there are two moves (one into parameter and another into vector). The conversion produces an rvalue (i. g. How to fix depends on what the return type of cleverConfig. 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. The type of such a reference must be a const qualified lvalue reference or a rvalue references. CheckCollision(0. Non-const reference may only be bound to an lvalue. 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). For example inc(1). Your declaration of a is a non-const lvalue reference,. Follow edited Apr 5, 2021 at 12:41. 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. and not. @Nater The kind of reference (const/lvalue/rvalue) is irrelevant to the lifetime extension rules. The reference returned from get_value is bound to x which is an l-value, and that's allowed. The temporary unsigned int could be bound to lvalue-reference to const (i. Maybe because you're not doing anything the call is optimized away. Share. Specifically, a const rvalue will prefer to bind to the const rvalue reference rather than the const lvalue reference. e. There are exceptions, however. In the second case, fun() returns a non-const lvalue reference, which can bind to another non-const reference, of course. const int *p; - here it is pointer on const int int const *p; - here it is const pointer on int const int const *p; -. In your code, int & is a non-const lvalue reference. Changing it to void display (const double& arg) works because everything works the same as explained above. Share. An lvalue (locator value) represents an object that occupies some identifiable location in memory (i. 3 -- Lvalue references ), we discussed how an lvalue reference can only bind to a modifiable lvalue. First of all, I will post the warning I'm getting: xlist. thanks in advance, George. initial value of reference to non-const must be an lvalue, Passing an object type by. If you want to capture the reference you need to declare a reference. The only way to safely bind an rvalue to an lvalue is either by. The this pointer is defined to be a prvalue, and your function takes an lvalue. The advantage of rvalue references over lvalue references is that with rvalue references you know that the object referred to is an rvalue. The parameter of the function is an lvalue reference to non-const, and such references cannot be bound to rvalues 1. But a is an lvalue expression because it refers to an object's name . 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. Of course, unlike the one in the range-based for loop, this i reference become dangling immediately. add (std::move (ct)); } A forwarding reference can bind to both lvalues and rvalues, but. int a = 7. lvalue reference 는 “data type. In C++03 the only reason to use the const& trick is in the case where. Add a comment. The second const is good, as is stops the source item being modified. The answer to the question in the title is: yes, the copy-constructor can have a non-const argument. T may resolve to different types of reference, but the type trait don't know about references. There are two overloads. cannot bind non-const lvalue reference of type to an rvalue of type. push() can use an if constexpr. 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. In the case of storing a value, the type can be non-const and the function could modify that object, so the approach is more flexible. The core of your question is: can rvalues be bound to non-const lvalue references?. reference to type 'myclass' could not bind to an rvalue of type 'myclass *'. Only const lvalue references (in C++98 and C++11) or rvalue references (in C++11 only) can. 1. c++; Share. non-const reference of type from an rvalue. Follow edited May 23, 2017 at 11:55. (PS the lifetime of the temporary is extended to the lifetime of the reference. If t returns by lvalue reference, the code does not compile because a rvalue reference cannot bind to it. However, an rvalue can be bound to a. 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. 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. C4239 は、以下。. A reference may be bound only to an object, not to literal or to result of expression . A non-const reference may only be bound to an lvalue[/quote] Reply Quote 0. Troubles understanding const in c++ (cannot bind non-const lvalue reference) 0. I don't get why the make_range function doesn't work unless I remove the View (View<C>& r) constructor. thanks in advance, George. The linked page uses the words "rvalue" and "lvalue" incorrectly . 6 — Pass by const lvalue reference. Non-const reference may only be bound to an lvalue (2 answers) Error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’ (2 answers) If you have a temporary object that's very expensive to copy, you may prefer to take a const& to that object (say a function return) rather than copying it into another variable to use later. 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. std::vector<bool> is special from all other std::vector specializations. 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. yet you can still change the data x by modifying x. 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&&. But a more proper fix is to change the parameter to a const reference:However, you might need at that returns non-const reference too. An expression that designates a bit field (e. 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. & attr (optional) declarator. ) But there is no way to show me how to solve it;You may modify a non-const object through a non-const reference. is an xvalue, class prvalue, array prvalue or function lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or. e. { A res; res. Thank you. 3 Answers. Regarding the second question. You can normally hide the expression template type behind private members. The relevant part of the standard is in [class. e. A non-const reference may only be bound to an lvalue[/quote] 1 Reply Last reply Reply Quote 0. However, C++ makes one exception to this rule and allows const lvalue references to also bind to rvalues. C++ prohibits passing a temporary object as a non-const reference parameter. Values are fine: auto refInstance = m_map. 3. 1. If /Zc:referenceBinding is specified, the compiler follows section 8. This sample shows the Microsoft extension that allows a temporary of a user-defined type to be bound to a non-const lvalue reference. The solution depends on the value of return type in cleverConfig. 3. 10 is a prvalue expression. ;, x is an lvalue denoting a float whose value is the result of converting a to double and back. GetCollider (); platform1. The whole idea of forwarding is to accept any value category and preserve it for future calls. If t returns by rvalue reference, you obtain a reference to whatever was returned. You can't. The lifetime extension is not transitive through a. e. 71. an lvalue, this constructor cannot be used, so the compiler is forced to use. Thus, in the case where P is const T&& (which is not a forwarding reference), it is transformed to const T and whether or not the argument is an lvalue doesn't affect the type deduction, since value. So your reference would be referring to the copy of the pointer which wouldn't be modified if you change the Player object. Hence, B::B (A) will be selected, because there is a conversion from B to A. The basic idea behind references is that lvalue references bind to lvalues, and rvalue references bind to rvalues; the reference thus bound henceforth refers to the value it was bound to. 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. Non. 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. The Rvalue refers to a value stored at an address in the memory. std::tie always expects lvalues for arguments, since its intended purpose is to be used in assignment. 3. 4. have a good weekend, George. 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. 3. In your default constructor, you try to assign a temporary value (the literal 0) to it, and since it's a reference type you can't give it a temporary value. The implication of a function that takes a non-const reference as an argument is that there is a side-effect applied to the value of that argument. C++/SDL "initial value of reference to a non-const must be an lvalue". it is explained that an lvalue is when you can take its address. 25th May 2022, 8:44 AM. 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. The code below is not legal (problem with the foo_t initializer list) because: "A reference that is not to 'const' cannot be bound to a non-lvalue" How can I best achieve an. A temporary has a type, that type can be const, and it can be non-const. The parameter list for a move constructor, however, consists of an rvalue reference, like B&& x. My question is, why a non-const reference can not binded to a rvalue? I think the reason is rvalue is not addressable? And we can not change the rvalue through its reference?Warning: "A non-const reference may only be bound to an lvalue" I've encountered a very weird warning that, although compiles fine on windows, fails to. e. ref]/5: — Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i. a. Improve this answer. This may sound like a silly question, but I was confused about this following behaviour:. g. “An old special-case permits an rvalue to be bound to an lvalue reference to non-const type when that reference is the. Pass by reference can only accept modifiable lvalue arguments. for example, to get a reference to the element. 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. 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. 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. (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. Non-const reference may only be bound to an lvalue. ii. 1 invalid initialization of non-const reference of type from an rvalue of type. e. A non-const reference can be used to change the value of the variable it is referring to. The reference in your example is bound to the constructor's argument n, and becomes invalid when the object n is bound to goes out of scope. int const (& crb)[3] = b; here we have reference to array of const int, we can also write const int (& crb)[3] = b; It would be the same. It can take rvalues because it is marked const and rvalues are allowed to bind to const lvalue references. Non-const reference may only be bound to an lvalue. 19 tricky. It's just that type of that lvalue is "rvalue reference to Key ". 3. g. 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. 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. int x; int&& r = x; but also. Sometimes even for the original developer, but definitely for future maintainers. is an xvalue, class prvalue, array prvalue or function lvalue and "cv1 T1" is reference-compatible with "cv2 T2", or. Universal references is a technique. 7. An lvalue reference is declared using the & operator, for example int& . 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. The reason for this is mostly convenience: It. Given all three functions, this call is ambiguous. In such cases: [1] First, implicit type conversion to T is applied if necessary. However, getPlayer is returning a copy of that pointer. The type of such a reference must be a const qualified lvalue reference or a rvalue references. If t returns by lvalue reference, the code does not compile because a rvalue reference cannot bind to it. at returns a proxy (of type std::vector<bool>::reference) that allows you to write the element. including the case where an lvalue is provided, it cannot modify its input (at least not the one bound to the x parameter) - if it did, it would violate the semantics. In the following post: Understanding lvalue/rvalue expression vs object type. Suppose r is an rvalue reference or non-volatile const lvalue reference to type T, and r is to be initialized by an expression e of type U. clang++ says: " error: non-const lvalue reference to type 'class foo' cannot bind to a temporary of type 'class foo'" Change foo. With either, you do not have a (local) guarantee that the object will not be manipulated elsewhere. An rvalue reference can only bind to non-const rvalues. Share. Only const lvalue references (in C++98 and C++11) or rvalue references (in C++11 only) can. You have two options, depending on your intention. Use a const reference, which can be bound to rvalues. Mar 22, 2013 at 18:39. 1. 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. Actually the precise reason it doesn't work is not because temporaries cannot be bound to non-const lvalue references, but rather that the initializer of a non-const lvalue reference is subject to certain requirements that char const[N] cannot meet in this case, [dcl. 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. @acannon828 Okay, but then you'd be modifying the pointer that is internal to World. In other words, in your first example the types actually do match. Universal reference, or forwarding reference, only happen because of reference collapsing. Returning non-const lvalue reference. A reference to the container element is obtained from the iterator with the indirection operator: *hand_it. 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. In fact, in terms of overload resolution, an rvalue prefers to be bound to an rvalue reference than to an lvalue const reference. From the C++20 draft. So you want x to be either an. r-value references are designed to be the subject of a move-constructor or move-assignment. 5. We can't bind non-const lvalue reference to an rvalue, but it can be bound to the const one. Here you are taking a reference to a uint8Vect_t. 4. A const lvalue reference or rvalue reference can be. 5. 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. Follow edited Oct 5 at. 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'. A function lvalue; If an rvalue reference or a nonvolatile 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. 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. The problem is that auto and decltype side-step the whole public/private thing, allowing you to create types that you. There is no need for references. The temporary int's lifetime will be the same as the const reference. Sometimes even for the original developer, but definitely for future maintainers. g. If it is not immediately obvious, we can try to check: Therefore, you can opt to change your getPtr ()'s return to a non-const lvalue reference. So, in C++ (as in C) a = &b gets a pointer to b and stores this value in a, so if b is of type int, a needs to be of type int*. All groups and messages. Expect the programmer to take extra care to modify values only via those references which do not refer to literals, and invoke undefined behaviour if you get it wrong. Sounds like you actually want getPlayer to return a reference too and then to. I have fixed these issues and completely understand how/why it gives a warning. initial value of reference to non-const must be an lvalue. 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. When I discovered this, it seemed odd to me, so I tried. In the original example , both are xvalues so the ternary operator evaluates to an xvalue. No, "returning a reference" does not magically extend any lifetime. match. If t were really an out-parameter, it would be passed by pointer: std::string *t. 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. reference (such as the B& parameter in the B::B (B&) constructor) can only. That is to say, usage of a reference is syntactically identical to usage of the referent. –The pointer returned by the function cannot be bound to a reference. And const is a constraint imposed by the compiler to the variable that is declared as const. My understanding is that this is largely to avoid breaking several enormous legacy codebases that rely on this "extension. Non-compliant compilers might allow a non-const or volatile lvalue reference to be bound to an rvalue. 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). 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. You can call a non-const member function on a temporary because this does not involve binding of a reference. ; T is not reference-related to U. If t returns a local variable, then you get a dangling reference, since that variable is gone after the call. Saturday, December 15, 2007 4:49 AM. , cv1 shall be const), or the reference shall be an rvalue reference. 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. Share. You are returning a copy of A from test so *c triggers the construction of a copy of c. The int* needs to be converted to void* firstly, which is a temporary object and could be bound to rvalue-reference. 2/5 in N4140): A temporary bound to a reference parameter in a function call (5. a. Notes: A non-const or volatile lvalue reference cannot be bound to anrvalue of a built-in type. e. Saturday, December 15, 2007 4:49 AM. at(0) = false; The reaons is that x. I could even (though this is a bit unusual) safely const_cast away the constness of b, since I also hold a non-const reference to the same object. not an rvalue reference, everything under the sun can be bound by a forwarding reference – Piotr Skotnicki. Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i. So you cannot change the data of x with reference variable r (just acts a read only). 4 — Lvalue references to const. , cv1 shall be const), or the reference shall be an rvalue reference. A temporary can only bind to const lvalue references, or rvalue references. error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int' GCC complains about the reference not being const, namely a constant. Find more info here. The page is trying to say that you can write m. A reference (of any kind) is just an alias for the referenced object. struct Foo{}; { const auto & r = Foo{}; // Foo object not destroyed at semicolon. 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. Non-explicit constructors have their uses. 3/5:. So, when you type const int& ref = 40. Unless an object is created in the read-only section of a program, it is open for modifiction without adverse consequences. GetImage (iTileId, pulImageSize, a_pImage ); With the method defined as: This change is required by the C++ standard which specifies that a non-const. 1 Answer. 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. What you probably want is: BYTE *pImage = NULL; x. if a. By using the const keyword when declaring an lvalue reference, we tell an lvalue reference to treat the object it is referential when const. This means the following is illegal: This is disallowed because it would allow us to modify a const variable ( x) through the non-const reference ( ref ). 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. Thank you for answering. GetCollider(). In the second case, fun() returns a non-const lvalue reference, which can bind to another non-const reference, of course. Oct 10, 2013 at 22:07. std::is_rvalue_reference<T&&>::valueA temporary can only bind to a reference to a prvalue. I am aware that a non-const reference can't bind to a temporary, but I really don't see why x2 can be considered as one and not x1. Unlike a reference to non-const (which can only bind to modifiable lvalues), a reference to. rval] is not applied (i. And the lvalue-reference to const could bind to. , you may only want to hold on to a const Bar*, in which case you then can also only pass a const Bar*) Using a const Bar& as parameter type is bound to result in a runtime crash sooner rather than later because:The C++ Standard (2003) indicates that an rvalue can only be bound to a const non-volatile lvalue reference. Otherwise, the reference you get behaves more. 5 The first option can take lvalues because it's an lvalue reference. (Binding to a const reference is allowed. 12. " I really need some further explanations to solving this: #include "graph1. Non-const lvalue reference to type 'Common::XYZCallbackInterface' cannot bind to a temporary of type 'Common::XYZCallbackInterface *'. 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. 0 Invalid initialization of non-const reference from a. Your code has two problems. e. But in your case the operands are different category (123 is a prvalue, a is an lvalue). ref/6] ). 1. Lvalue and rvalue expressions. Value categories pertain to expressions, not objects. int&& x = 10; is a declaration and not an expression. 2), an xvalue if T is an rvalue reference to object type, and a prvalue otherwise. Actually for simple types you should prefer to. 1. rvalue references are marked with two ampersands (&&). it doesn't say anything else. Saturday, December 15, 2007 4:49 AM. decltype(fun()) b=1; Then, your code initializes a const reference with a prvalue of a different (non-reference-related) type. U is a class type. // zcreferencebinding. If an rvalue could bind to a non-const lvalue reference, then potentially many modifications could be done that would eventually be discarded (since an rvalue is temporary), this being useless. Don't pass int&, it can't be bound to a constant or temporary because those can't be modified - use const int& instead. We can't bind rvalue reference to an lvalue also. If t returns a local variable, then you get a dangling reference, since that variable is gone after the call. A variable is an lvalue, so you are allowed to bind a non const reference to it. ref]/5:. and if you pass it to a function that takes a reference to a non-const - it means that function can change the value. Regarding the second question. int const&x = 42; // It's ok. That's only proper when the type is const and the context is one where there is automatic lifetime extension of the temporary. 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. 4. Within the body of a non-static member function of X, any id-expression e (e. 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. The method forward has const in its parameter, so the int& version should have the parameter const int& t.