Reg. Doubt in C++ References

Discussion in 'C++' started by VSP, Jan 15, 2007.

  1. VSP

    VSP Guest

    Hi,

    I have a doubt regarding using references.
    Please look at the below code. I am using VC++ 6.0

    int &i = 10; // Compilation error

    error C2440: 'initializing' : cannot convert from 'const int' to 'int &'
    A reference that is not to 'const' cannot be bound to a non-lvalue

    Assume that "Test" is a class
    Test &t = Test(); // No compilation error

    Why for built-in types assigning non-const reference with a non-lvalue is
    giving error and for the
    User defined type it is not giving any error?

    Thanks
    VSP
    VSP, Jan 15, 2007
    #1
    1. Advertising

  2. VSP

    Kai-Uwe Bux Guest

    VSP wrote:

    > I have a doubt regarding using references.
    > Please look at the below code. I am using VC++ 6.0
    >
    > int &i = 10; // Compilation error
    >
    > error C2440: 'initializing' : cannot convert from 'const int' to 'int &'
    > A reference that is not to 'const' cannot be bound to a non-lvalue
    >
    > Assume that "Test" is a class
    > Test &t = Test(); // No compilation error
    >
    > Why for built-in types assigning non-const reference with a non-lvalue is
    > giving error and for the
    > User defined type it is not giving any error?


    According to the standard, you should see an error in both cases.
    Apparently, your compiler is broken. Try:

    struct Test {};

    int main ( void ) {
    Test & t = Test();
    }


    at: http://www.comeaucomputing.com/tryitout/



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jan 15, 2007
    #2
    1. Advertising

  3. VSP

    Guest


    > I have a doubt regarding using references.
    > Please look at the below code. I am using VC++ 6.0
    >
    > int &i = 10; // Compilation error
    >
    > error C2440: 'initializing' : cannot convert from 'const int' to 'int &'
    > A reference that is not to 'const' cannot be bound to a non-lvalue
    >
    > Assume that "Test" is a class
    > Test &t = Test(); // No compilation error
    >
    > Why for built-in types assigning non-const reference with a non-lvalue is
    > giving error and for the
    > User defined type it is not giving any error?


    Your compiler is broken.
    Try your sample on Comeau online -
    http://www.comeaucomputing.com/tryitout
    m
    , Jan 15, 2007
    #3
  4. VSP

    Zorro Guest

    Kai-Uwe Bux wrote:
    > VSP wrote:
    >
    > > I have a doubt regarding using references.
    > > Please look at the below code. I am using VC++ 6.0
    > >
    > > int &i = 10; // Compilation error
    > >
    > > error C2440: 'initializing' : cannot convert from 'const int' to 'int &'
    > > A reference that is not to 'const' cannot be bound to a non-lvalue
    > >
    > > Assume that "Test" is a class
    > > Test &t = Test(); // No compilation error
    > >
    > > Why for built-in types assigning non-const reference with a non-lvalue is
    > > giving error and for the
    > > User defined type it is not giving any error?

    >
    > According to the standard, you should see an error in both cases.
    > Apparently, your compiler is broken. Try:
    >
    > struct Test {};
    >
    > int main ( void ) {
    > Test & t = Test();
    > }
    >
    >
    > at: http://www.comeaucomputing.com/tryitout/
    >
    >
    >
    > Best
    >
    > Kai-Uwe Bux


    Let us look at this from a different angle, before dismissing it. Note
    that, at "Test()" an object is in fact created, within the same scope
    as the reference to it. So, within that scope, t can indeed reach the
    object. This is not in voilation of object-oriented view, though it may
    violate the standard.
    However, C++ takes the C built-in types as is. Then, the literal 10 is
    not an object (not an l-value for C). There is nothing to point to
    because a literal for a built-in is only available during compilation
    unless it is assigned to an object (of its type).

    Regards,

    http://www.zhmicro.com
    http://distributed-software.blogspot.com
    http://groups-beta.google.com/group/computerlangZ?lnk=la&hl=en
    Zorro, Jan 15, 2007
    #4
  5. VSP

    Dizzy Guest

    Zorro wrote:

    > Let us look at this from a different angle, before dismissing it. Note
    > that, at "Test()" an object is in fact created, within the same scope
    > as the reference to it. So, within that scope, t can indeed reach the
    > object. This is not in voilation of object-oriented view, though it may
    > violate the standard.


    So you wonder what is the reasoning behind that decision in the standard ?

    > However, C++ takes the C built-in types as is. Then, the literal 10 is
    > not an object (not an l-value for C). There is nothing to point to
    > because a literal for a built-in is only available during compilation
    > unless it is assigned to an object (of its type).


    But for references to const C++ allows to bind to a literal like that.

    int const& i = 10;
    (the compiler will create a temporary in the same scope)

    It's someway unified that references to const are allowed to temporaries
    even to literals like that and it makes programming typeless interfaces (ie
    templates) easier.

    --
    Dizzy
    http://dizzy.roedu.net
    Dizzy, Jan 15, 2007
    #5
  6. VSP

    Zorro Guest

    Dizzy wrote:
    > Zorro wrote:
    >
    > > Let us look at this from a different angle, before dismissing it. Note
    > > that, at "Test()" an object is in fact created, within the same scope
    > > as the reference to it. So, within that scope, t can indeed reach the
    > > object. This is not in voilation of object-oriented view, though it may
    > > violate the standard.

    >
    > So you wonder what is the reasoning behind that decision in the standard ?
    >
    > > However, C++ takes the C built-in types as is. Then, the literal 10 is
    > > not an object (not an l-value for C). There is nothing to point to
    > > because a literal for a built-in is only available during compilation
    > > unless it is assigned to an object (of its type).

    >
    > But for references to const C++ allows to bind to a literal like that.
    >
    > int const& i = 10;
    > (the compiler will create a temporary in the same scope)
    >
    > It's someway unified that references to const are allowed to temporaries
    > even to literals like that and it makes programming typeless interfaces (ie
    > templates) easier.
    >
    > --
    > Dizzy
    > http://dizzy.roedu.net


    The compiler error already indicated that it could do it for const. How
    is const related to what I said?
    I am aware of C++ templates accepting literals like "10", instead of
    types. That is because C++ template instantiation is done the same way
    C macros are, i.e. text editing. I am not objecting to this, just
    saying how it works, and that is the way one should accept it.

    Please note that I was explaining why VC++ did not generate error for
    Test(). That is because, Test() is an object (after elaboration). There
    is no const involved in my response.

    Thanks for your comment, though.
    Regards.
    Zorro, Jan 16, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    604
  2. doubt reg MAC id

    , Jan 25, 2008, in forum: Java
    Replies:
    2
    Views:
    302
    Roedy Green
    Jan 25, 2008
  3. Replies:
    0
    Views:
    543
  4. Peter Otten
    Replies:
    2
    Views:
    104
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    109
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page