Thread: A non-const reference may only be bound to an lvalue?

  1. #1
    Registered User
    Join Date
    May 2006

    A non-const reference may only be bound to an lvalue?

    Hello everyone,

    I am debugging MSDN code from,

    here is my output,

    1>d:\visual studio 2008\projects\test_c4350\test_c4350\main.cpp(21) : warning C4350: behavior change: 'B::B(A)' called instead of 'B::B(B &)'
    1> d:\visual studio 2008\projects\test_c4350\test_c4350\main.cpp(13) : see declaration of 'B::B'
    1> d:\visual studio 2008\projects\test_c4350\test_c4350\main.cpp(9) : see declaration of 'B::B'
    1> A non-const reference may only be bound to an lvalue

    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 &)'? Could anyone give some descriptions please?

    thanks in advance,

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Hannover, Germany
    The site you got the code from is the explanation why this warning appears, it's the example code for reproducing it. I'll try paraphrasing it:

    In compiler version 2002, if the compiler had the choice if it would transform an object returned by a function to a non-const-reference or another type, it would have chosen the non-const-reference. 2005 and better will instead opt to cast the value to another type that is passed either by value or by const reference instead. Why ? Well, passing a value that will vanish anyway to a function that will write to that value makes no sense. No one will ever see the results.

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    In the poster's favor though Microsoft does come up with some fairly cryptic error messages along with equally cryptic explanations in their help files.

    Today we loaded code in C# and the form designer siad it could not load. The error message was long and cryptic and left all of us developers sitting there scratching our heads.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM