Is temporary variable always *const*?

Discussion in 'C++' started by Morgan Cheng, Aug 10, 2005.

  1. Morgan Cheng

    Morgan Cheng Guest

    Hi,

    This isssue consfuse me.

    According to <Thinking in C++>, temporay variable is *const*. like

    class X{};
    X f(){return X();}
    void g1(X&){}
    void g2(const X&){}
    int main()
    {
    g1(f() );//Not Ok
    g2(f() );//OK,const
    }

    However, it is also said temporay variable can be modified.
    class Y{
    public:
    Y(int ii){i=ii;}
    void incr(){i++;}
    private:
    int i;
    };

    Y f(){return Y(0);}

    main(){
    f().incr();//change
    }

    Anybody can explan this?


    Thanks
    Morgan Cheng, Aug 10, 2005
    #1
    1. Advertising

  2. * Morgan Cheng:
    > According to <Thinking in C++>, temporay variable is *const*.


    Do you have a reference to such statement?


    > like
    >
    > class X{};
    > X f(){return X();}
    > void g1(X&){}
    > void g2(const X&){}
    > int main()
    > {
    > g1(f() );//Not Ok
    > g2(f() );//OK,const
    > }


    f() produces an rvalue (in C that meant a value that could be on the
    right-hand side of a assignment, but not on the left-hand side; C++ has lots
    of exceptions to that rule, but it's the conceptual underpinning). An
    rvalue cannot be bound directly to a reference to non-const. I'm not sure
    exactly why; e.g., the often quoted possibility of modifying the number 42
    really doesn't hold up on closer inspection.

    An rvalue can be const or non-const.

    The above rvalue is non-const, but there are no ways of exploiting that for
    class X.

    However, if you changed class X to

    class X{ public: operator X&() { return *this; } };

    you'd make the code compile.

    Here the rvalue is not bound directly: it's converted to a reference to
    non-const X via the defined operator, and that reference is then bound.


    > However, it is also said temporay variable can be modified.
    > class Y{
    > public:
    > Y(int ii){i=ii;}
    > void incr(){i++;}
    > private:
    > int i;
    > };
    >
    > Y f(){return Y(0);}
    >
    > main(){
    > f().incr();//change
    > }


    This is allowed as a special case. If you declared the f result type as 'Y
    const' it would be forbidden. Some years ago that was recommended practice
    (after all, who'd want to intentionally change a function result value?),
    but for various reasons -- it wasn't really helpful and in some cases it
    could be a hindrance -- it's not recommended today.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 10, 2005
    #2
    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. Replies:
    11
    Views:
    1,086
  2. Javier
    Replies:
    2
    Views:
    543
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,090
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    871
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    526
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page