temporory objects are constant ?

Discussion in 'C++' started by MagnumOpus, Jul 6, 2010.

  1. MagnumOpus

    MagnumOpus Guest

    Hello all,

    I have a question on temporary objects.
    Are the temporary objects always constant ?

    class X {
    int i;
    public:
    X (int ii = 0);
    void modify();
    };

    X::X(int ii) { i = ii; }

    void X::modify() { i = i++; }

    X f5() { return X(); }

    void f7(X& x) { //return by const reference
    x.modify();
    }

    void main()
    {
    f7(f5());
    }

    As per Bruce Eckel, temporary objects are always constant.
    But when i tried to compile this prograrm in VS, it doesn't complain
    me at all.

    Can anybody explain about this ?

    Thank you
    Sunil
    MagnumOpus, Jul 6, 2010
    #1
    1. Advertising

  2. MagnumOpus

    Ian Collins Guest

    On 07/ 6/10 02:08 PM, MagnumOpus wrote:
    > Hello all,
    >
    > I have a question on temporary objects.
    > Are the temporary objects always constant ?
    >
    > class X {
    > int i;
    > public:
    > X (int ii = 0);
    > void modify();
    > };
    >
    > X::X(int ii) { i = ii; }
    >
    > void X::modify() { i = i++; }
    >
    > X f5() { return X(); }
    >
    > void f7(X& x) { //return by const reference


    This comment doesn't make sense; the function does not return anything!

    > x.modify();
    > }
    >
    > void main()
    > {
    > f7(f5());
    > }
    >
    > As per Bruce Eckel, temporary objects are always constant.
    > But when i tried to compile this prograrm in VS, it doesn't complain
    > me at all.


    The compiler isn't enforcing the rule (is it a recent version?). It is
    invalid to initialise a non-const reference with a temporary.

    The compiler should be complaining about the incorrect definition of main.

    --
    Ian Collins
    Ian Collins, Jul 6, 2010
    #2
    1. Advertising

  3. * MagnumOpus, on 06.07.2010 04:08:
    >
    > I have a question on temporary objects.
    > Are the temporary objects always constant ?


    No.


    > class X {
    > int i;
    > public:
    > X (int ii = 0);
    > void modify();
    > };
    >
    > X::X(int ii) { i = ii; }
    >
    > void X::modify() { i = i++; }
    >
    > X f5() { return X(); }
    >
    > void f7(X& x) { //return by const reference
    > x.modify();
    > }
    >
    > void main()


    This should not compile, since 'main' must have 'int' result type.

    At a guess you're using MSVC.


    > {
    > f7(f5());


    This should not compile, since a temporary can't implicitly convert to reference
    to non-const.

    At a guess you're using MSVC.


    > }
    >
    > As per Bruce Eckel, temporary objects are always constant.


    Probably that is is not what he's written.


    > But when i tried to compile this prograrm in VS, it doesn't complain
    > me at all.
    >
    > Can anybody explain about this ?


    First, temporary objects are not const, except those you explicitly create as
    const. Your code above should still not compile. That it does compile probably
    means that you're using MSVC.

    You should consider upping the MSVC warning level to at least /W3.


    Cheers & hth.,

    - ALf


    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Jul 6, 2010
    #3
  4. MagnumOpus

    Öö Tiib Guest

    On 6 juuli, 05:08, MagnumOpus <> wrote:
    > Hello all,
    >
    > I have a question on temporary objects.
    > Are the temporary objects always constant ?
    >
    > class X {
    >         int i;
    > public:
    >         X (int ii = 0);
    >         void modify();
    >
    > };
    >
    > X::X(int ii) { i = ii; }
    >
    > void X::modify() { i = i++; }
    >
    > X f5() { return X(); }
    >
    > void f7(X& x) { //return by const reference
    >         x.modify();
    >
    > }
    >
    > void main()
    > {
    >     f7(f5());
    >
    > }
    >
    > As per Bruce Eckel, temporary objects are always constant.
    > But when i tried to compile this prograrm in VS, it doesn't complain
    > me at all.
    >
    > Can anybody explain about this ?


    VS gives a warning (at warning level 4):

    warning C4239: nonstandard extension used : 'argument' : conversion
    from 'X' to 'X &'

    Typical VS. Important warnings need level 4; worthless sillywarnings
    are given at level 3. Use always /W4.
    Öö Tiib, Jul 6, 2010
    #4
  5. MagnumOpus

    Öö Tiib Guest

    On 6 juuli, 05:42, Öö Tiib <> wrote:
    > On 6 juuli, 05:08, MagnumOpus <> wrote:
    >
    >
    >
    > > Hello all,

    >
    > > I have a question on temporary objects.
    > > Are the temporary objects always constant ?

    >
    > > class X {
    > >         int i;
    > > public:
    > >         X (int ii = 0);
    > >         void modify();

    >
    > > };

    >
    > > X::X(int ii) { i = ii; }

    >
    > > void X::modify() { i = i++; }

    >
    > > X f5() { return X(); }

    >
    > > void f7(X& x) { //return by const reference
    > >         x.modify();

    >
    > > }

    >
    > > void main()
    > > {
    > >     f7(f5());

    >
    > > }

    >
    > > As per Bruce Eckel, temporary objects are always constant.
    > > But when i tried to compile this prograrm in VS, it doesn't complain
    > > me at all.

    >
    > > Can anybody explain about this ?

    >
    > VS gives a warning (at warning level 4):
    >
    >  warning C4239: nonstandard extension used : 'argument' : conversion
    > from 'X' to 'X &'


    Did not somehow paste important part of warning text:

    A non-const reference may only be bound to an lvalue
    Öö Tiib, Jul 6, 2010
    #5
  6. MagnumOpus

    Rolf Magnus Guest

    MagnumOpus wrote:

    > Hello all,
    >
    > I have a question on temporary objects.
    > Are the temporary objects always constant ?


    No.

    struct X
    {
    int i;
    };

    int main()
    {
    X().i = 5;
    }


    > As per Bruce Eckel, temporary objects are always constant.


    I heavily doubt that.

    > But when i tried to compile this prograrm in VS, it doesn't complain
    > me at all.


    Your compiler is probably not configured correctly. You might need to set it
    to "standard compliant mode" or something like that.
    Rolf Magnus, Jul 6, 2010
    #6
  7. MagnumOpus

    gwowen Guest

    On Jul 6, 3:08 am, MagnumOpus <> wrote:

    > void X::modify() { i = i++; }


    Don't do that... use one of { i = i+1; } or { ++i; } or { i++; }
    gwowen, Jul 6, 2010
    #7
  8. MagnumOpus

    MagnumOpus Guest

    On Jul 6, 7:25 am, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * MagnumOpus, on 06.07.2010 04:08:
    >
    >
    >
    > > I have a question on temporaryobjects.
    > > Are the temporaryobjectsalwaysconstant?

    >
    > No.
    >
    >
    >
    >
    >
    > > class X {
    > >    int i;
    > > public:
    > >    X (int ii = 0);
    > >    void modify();
    > > };

    >
    > > X::X(int ii) { i = ii; }

    >
    > > void X::modify() { i = i++; }

    >
    > > X f5() { return X(); }

    >
    > > void f7(X&  x) { //return by const reference
    > >    x.modify();
    > > }

    >
    > > void main()

    >
    > This should not compile, since 'main' must have 'int' result type.
    >
    > At a guess you're using MSVC.
    >
    > > {
    > >      f7(f5());

    >
    > This should not compile, since a temporary can't implicitly convert to reference
    > to non-const.
    >
    > At a guess you're using MSVC.
    >
    > > }

    >
    > > As per Bruce Eckel, temporaryobjectsare alwaysconstant.

    >
    > Probably that is is not what he's written.
    >
    > > But when i tried to compile this prograrm in VS, it doesn't complain
    > > me at all.

    >
    > > Can anybody explain about this ?

    >
    > First, temporaryobjectsare not const, except those you explicitly create as
    > const. Your code above should still not compile. That it does compile probably
    > means that you're using MSVC.
    >
    > You should consider upping the MSVC warning level to at least /W3.
    >
    > Cheers & hth.,
    >
    > - ALf
    >
    > --
    > blog at <url:http://alfps.wordpress.com>- Hide quoted text -
    >
    > - Show quoted text -


    Hi

    The below is the program that i got it from Bruce Eckel and i comiled
    it in MSVC.

    /////////////////////////////////////////////////////////////////////

    class X {
    int i;
    public:
    X(int ii = 0);
    void modify();
    };
    X::X(int ii) { i = ii; }
    void X::modify() { i++; }
    X f5() {
    return X();
    }
    const X f6() {
    return X();
    }
    void f7(X& x) { // Pass by non-const reference
    x.modify();
    }
    int main() {
    f5() = X(1); // OK -- non-const return value
    f5().modify(); // OK
    // Causes compile-time errors:
    //! f7(f5());
    //! f6() = X(1);
    //! f6().modify();
    //! f7(f6());
    } ///:~

    ///////////////////////////////////////////////////////

    But when I uncommented f7(f5()), it doesn't complain me any errors.
    However, when i change the warning level to /W4, it says
    warning C4239: nonstandard extension used : 'argument' : conversion
    from 'X' to 'X &'

    I was expecting a error though.
    MagnumOpus, Jul 6, 2010
    #8
  9. MagnumOpus

    MagnumOpus Guest

    On Jul 6, 7:25 am, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * MagnumOpus, on 06.07.2010 04:08:
    >
    >
    >
    > > I have a question on temporary objects.
    > > Are the temporary objects always constant ?

    >
    > No.
    >
    >
    >
    >
    >
    > > class X {
    > >    int i;
    > > public:
    > >    X (int ii = 0);
    > >    void modify();
    > > };

    >
    > > X::X(int ii) { i = ii; }

    >
    > > void X::modify() { i = i++; }

    >
    > > X f5() { return X(); }

    >
    > > void f7(X&  x) { //return by const reference
    > >    x.modify();
    > > }

    >
    > > void main()

    >
    > This should not compile, since 'main' must have 'int' result type.
    >
    > At a guess you're using MSVC.
    >
    > > {
    > >      f7(f5());

    >
    > This should not compile, since a temporary can't implicitly convert to reference
    > to non-const.
    >
    > At a guess you're using MSVC.
    >
    > > }

    >
    > > As per Bruce Eckel, temporary objects are always constant.

    >
    > Probably that is is not what he's written.
    >
    > > But when i tried to compile this prograrm in VS, it doesn't complain
    > > me at all.

    >
    > > Can anybody explain about this ?

    >
    > First, temporary objects are not const, except those you explicitly create as
    > const. Your code above should still not compile. That it does compile probably
    > means that you're using MSVC.
    >
    > You should consider upping the MSVC warning level to at least /W3.
    >
    > Cheers & hth.,
    >
    > - ALf
    >
    > --
    > blog at <url:http://alfps.wordpress.com>- Hide quoted text -
    >
    > - Show quoted text -


    Please refer to Bruce Eckel's Thinking in c++, Constants chapter which
    clearly says

    "But there is one thing about temporaries: they’re
    automatically const."

    Let me know if my understanding is wrong.
    MagnumOpus, Jul 6, 2010
    #9
  10. On Jul 6, 11:06 am, MagnumOpus <> wrote:
    >
    > Hi
    >
    > The below is the program that i got it from Bruce Eckel and i comiled
    > it in MSVC.
    >
    > /////////////////////////////////////////////////////////////////////
    >
    > class X {
    > int i;
    > public:
    > X(int ii = 0);
    > void modify();};
    >
    > X::X(int ii) { i = ii; }
    > void X::modify() { i++; }
    > X f5() {
    > return X();}
    >
    > const X f6() {
    > return X();}
    >
    > void f7(X& x) { // Pass by non-const reference
    > x.modify();}
    >
    > int main() {
    > f5() = X(1); // OK -- non-const return value
    > f5().modify(); // OK
    > // Causes compile-time errors:
    > //! f7(f5());
    > //! f6() = X(1);
    > //! f6().modify();
    > //! f7(f6());
    >
    > } ///:~
    >
    > ///////////////////////////////////////////////////////
    >
    > But when I uncommented f7(f5()), it doesn't complain me any errors.
    > However, when i change the warning level to /W4, it says
    > warning C4239: nonstandard extension used : 'argument' : conversion
    > from 'X' to 'X &'
    >
    > I was expecting a error though.


    Most compilers will give an error for that code, but Microsoft has
    decided, in all its wisdom, that they want to (continue to) support
    this kind of code.
    This decision directly implies that the strongest type of complaint
    they will give will be a warning message, and not an error.
    The reason is that, by convention, if the compiler gives out one or
    more error messages for a file, then the compiler will not produce an
    object file or any other form of executable code.

    For standard compliance, it is sufficient that a compiler produces
    some kind of diagnostic message, but the standard does not
    differentiate between errors, warnings and other informational
    messages.

    Bart v Ingen Schenau
    Bart van Ingen Schenau, Jul 6, 2010
    #10
  11. MagnumOpus

    MagnumOpus Guest

    On Jul 6, 2:23 pm, Bart van Ingen Schenau <>
    wrote:
    > On Jul 6, 11:06 am, MagnumOpus <> wrote:
    >
    >
    >
    >
    >
    >
    >
    > > Hi

    >
    > > The below is the program that i got it from Bruce Eckel and i comiled
    > > it in MSVC.

    >
    > > /////////////////////////////////////////////////////////////////////

    >
    > > class X {
    > > int i;
    > > public:
    > > X(int ii = 0);
    > > void modify();};

    >
    > > X::X(int ii) { i = ii; }
    > > void X::modify() { i++; }
    > > X f5() {
    > > return X();}

    >
    > > const X f6() {
    > > return X();}

    >
    > > void f7(X& x) { // Pass by non-const reference
    > > x.modify();}

    >
    > > int main() {
    > > f5() = X(1); // OK -- non-const return value
    > > f5().modify(); // OK
    > > // Causes compile-time errors:
    > > //! f7(f5());
    > > //! f6() = X(1);
    > > //! f6().modify();
    > > //! f7(f6());

    >
    > > } ///:~

    >
    > > ///////////////////////////////////////////////////////

    >
    > > But when I uncommented f7(f5()), it doesn't complain me any errors.
    > > However, when i change the warning level to /W4, it says
    > > warning C4239: nonstandard extension used : 'argument' : conversion
    > > from 'X' to 'X &'

    >
    > > I was expecting a error though.

    >
    > Most compilers will give an error for that code, but Microsoft has
    > decided, in all its wisdom, that they want to (continue to) support
    > this kind of code.
    > This decision directly implies that the strongest type of complaint
    > they will give will be a warning message, and not an error.
    > The reason is that, by convention, if the compiler gives out one or
    > more error messages for a file, then the compiler will not produce an
    > object file or any other form of executable code.
    >
    > For standard compliance, it is sufficient that a compiler produces
    > some kind of diagnostic message, but the standard does not
    > differentiate between errors, warnings and other informational
    > messages.
    >
    > Bart v Ingen Schenau- Hide quoted text -
    >
    > - Show quoted text -


    Thank you for the information.
    I will try with other compilers also.
    MagnumOpus, Jul 6, 2010
    #11
  12. * MagnumOpus, on 06.07.2010 11:11:
    >
    > Please refer to Bruce Eckel's Thinking in c++, Constants chapter which
    > clearly says
    >
    > "But there is one thing about temporaries: they’re
    > automatically const."
    >
    > Let me know if my understanding is wrong.


    Yes, he is. :)

    I'm attempting to CC this to Bruce Eckel so that he can put it in his errata
    list, assuming there is one. Unfortunately I don't have his private address in
    Thunderbird (I probably had it once) and his websites seem to be down. I found a
    generic "business" address by looking at his pages via the Wayback Engine's
    archive, and he goes through some hoops to discourage readers from sending him
    mail, apparently as a popular author he gets tons of mails; if you need it it's
    MindViewInc as username for gmail address, and as it turned out /that/ address
    was in my Thunderbird address list, no mention of Bruce though :)

    "Thinking in C++" is great for getting up to speed, but like all books it has erors.

    Example:


    <code>
    struct S { int x; };

    S foo() { S o = {1234}; return o; }

    int main()
    {
    foo() = S();
    }
    </code>


    This code should compile because even though foo returns a temporary it's a
    temporary of class type, and you can call methods, in particular the
    automatically generated assignment operator, even on temporary class type
    objects. If the object was const, though, this should not compile. One cannot
    assign to a const object unless it has a user-defined assignment operator that
    is const (this is seldom done, but sometimes for proxy objects).


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Jul 6, 2010
    #12
    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. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,074
  2. Martin Magnusson
    Replies:
    2
    Views:
    492
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    545
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    332
    Keith Thompson
    Dec 14, 2006
  5. Replies:
    13
    Views:
    12,897
    Kai-Uwe Bux
    Jan 22, 2007
Loading...

Share This Page