current standard is it legal?

Discussion in 'C++' started by puzzlecracker, Jul 12, 2005.

  1. is it possible to call nonconst member function on an unnamed temprary?

    ex:
    class String{
    public:
    void doSomething();

    };

    String createString();
    createString().doSomething();

    is it ok?

    thanks.
     
    puzzlecracker, Jul 12, 2005
    #1
    1. Advertising

  2. puzzlecracker

    red floyd Guest

    puzzlecracker wrote:
    > is it possible to call nonconst member function on an unnamed temprary?
    >
    > ex:
    > class String{
    > public:
    > void doSomething();
    >
    > };
    >
    > String createString();
    > createString().doSomething();
    >
    > is it ok?
    >
    > thanks.
    >

    No. An unnamed temporary is an r-value (aka const).
     
    red floyd, Jul 12, 2005
    #2
    1. Advertising

  3. puzzlecracker

    John Carson Guest

    "red floyd" <> wrote in message
    news:VEEAe.1149$
    > puzzlecracker wrote:
    >> is it possible to call nonconst member function on an unnamed
    >> temprary? ex:
    >> class String{
    >> public:
    >> void doSomething();
    >>
    >> };
    >>
    >> String createString();
    >> createString().doSomething();
    >>
    >> is it ok?
    >>
    >> thanks.
    >>

    > No. An unnamed temporary is an r-value (aka const).


    I don't think an rvalue is necessarily const. I don't think any class object
    is const unless declared that way. Comeau compiles the code without
    objection. However, it won't compile if createString returns const String.

    --
    John Carson
     
    John Carson, Jul 12, 2005
    #3
  4. puzzlecracker

    E. Mark Ping Guest

    In article <>,
    puzzlecracker <> wrote:
    >is it possible to call nonconst member function on an unnamed temprary?
    >
    >ex:
    >class String{
    >public:
    > void doSomething();
    >
    >};
    >
    >String createString();
    >createString().doSomething();
    >
    > is it ok?


    Yes.

    Section 3.10 paragraph 5:
    The result of calling a function that does not return a reference is
    an rvalue. User defined operators are functions, and whether such
    operators expect or yield lvalues is determined by their parameter and
    return types.

    Section 3.10 paragraph 10:
    An lvalue for an object is necessary in order to modify the object
    except that an rvalue of class type can also be used to modify its
    referent under certain circumstances. [Example: a member function
    called for an object (9.3) can modify the object.]

    (Section 9.3 refers to member functions.)

    --
    Mark Ping
     
    E. Mark Ping, Jul 12, 2005
    #4
  5. puzzlecracker

    sandSpiderX Guest

    Hi,

    I find the code to be Ok. Just the function doSomething() is called on
    temporary object, which gets destroyed, so maybe no use.

    Otherwise, its Ok.
    AFAIK, standards define temporary object creations , and you an do
    anything with them.

    SandX
     
    sandSpiderX, Jul 12, 2005
    #5
  6. puzzlecracker

    Wallking Guest

    It's ok

    The temporary on the full expression does't go out of scope, and the
    unamed temporary is not a constant, so this is valid. And I test it on
    the VC7.0, It's OK!
     
    Wallking, Jul 12, 2005
    #6
  7. "puzzlecracker" <> schrieb:

    > is it possible to call nonconst member function on an unnamed
    > temprary?


    On local variables it's possible, on unnamed temporaries not in every
    case. This depends on the compiler.

    > class String{
    > public:
    > void doSomething();
    >
    > };
    >
    > String createString();


    This line normally leads to errors on many compilers. Calling the
    standard constructor is done without the brackets:

    String createString;

    > createString().doSomething();


    This line contains also superfluous brackets. You mean

    String().doSomething(); // yes, brackets needed

    or

    createString.doSomething();

    T.M.
     
    Torsten Mueller, Jul 12, 2005
    #7
  8. Torsten Mueller wrote:
    > "puzzlecracker" <> schrieb:
    >> is it possible to call nonconst member function on an unnamed
    >> temprary?


    Yes. A temporary is non-const and thus you can call all member
    functions on it, including non-const ones. You cannot bind a
    temporary to a non-const reference but this is irrelevant to
    calling member functions.

    > On local variables it's possible, on unnamed temporaries not in every
    > case. This depends on the compiler.


    Can you please give an example where it is compiler-dependent
    whether you can call a non-const member function on a temporary?
    If you have found a compiler which does not allow this, I'm
    pretty sure it is an error for that specific compiler.

    >> String createString();

    >
    > This line normally leads to errors on many compilers.


    A local function declaration is definitely legal according to
    the standard. Thus, your statement effectively says that many
    compilers are broken. Can you please give the name of at least
    two compiler where the above line yields an error? ... after
    all, we want to avoid using them.

    > Calling the
    > standard constructor is done without the brackets:
    >
    > String createString;


    Given that 'createString()' is clearly used as function later,
    I think you got the use wrong. Although I think it was an error
    to allow local function declarations, it is legal according to
    the current standard.
    >
    >> createString().doSomething();

    >
    > This line contains also superfluous brackets.


    I think you entirely misunderstood what the original poster
    did! The first 'createString()' is a function declaration,
    not an object declaration. Actually, the name of the function
    even indicates what it may be intended to do...

    I'm, however, still interested in the broken compilers...
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
     
    Dietmar Kuehl, Jul 12, 2005
    #8
  9. red floyd wrote:
    > puzzlecracker wrote:
    >> is it possible to call nonconst member function on an unnamed temprary?
    >>
    >> ex:
    >> class String{
    >> public:
    >> void doSomething();
    >>
    >> };
    >>
    >> String createString();
    >> createString().doSomething();
    >>
    >> is it ok?


    Yes.

    > No. An unnamed temporary is an r-value (aka const).


    You should read about r-values again: they are not at all const!
    However, r-values cannot be bound to a non-const reference. This
    does not mean that they are const themselves.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
     
    Dietmar Kuehl, Jul 12, 2005
    #9
  10. Dietmar Kuehl <> schrieb:

    > > On local variables it's possible, on unnamed temporaries not in
    > > every case. This depends on the compiler.

    >
    > Can you please give an example where it is compiler-dependent
    > whether you can call a non-const member function on a temporary? If
    > you have found a compiler which does not allow this, I'm pretty sure
    > it is an error for that specific compiler.


    I had indeed a problem at least with older gcc-implementations (gcc
    3.2 and below, and also the with egcs compiler, it's years ago, I
    know). To solve this I had always to make an explicit local non-const
    copy of the const object while MSVC 6 and 7 did compile it without the
    copy. I'm really sure. I documented this problem in my private list of
    compiler problems.

    > > > String createString();

    > >
    > > This line normally leads to errors on many compilers.

    >
    > A local function declaration is definitely legal according to the
    > standard.


    Argh - damned - this should be a local function! My mistake ...

    > Actually, the name of the function even indicates what it may be
    > intended to do...


    Yes, sure. But I do never interpret function names too much. I'm very
    familiar with function names like "Get_getLeitwegFirst()" and it's
    beautiful opposit "Put_getLeitwegFirst()".

    T.M.
     
    Torsten Mueller, Jul 12, 2005
    #10
  11. puzzlecracker

    red floyd Guest

    Dietmar Kuehl wrote:

    > red floyd wrote:
    >> [blatant stupidity on my part redacted]


    > You should read about r-values again: they are not at all const!
    > However, r-values cannot be bound to a non-const reference. This
    > does not mean that they are const themselves.


    Whoops! My bad. I was thinking of ... OK, I have no f-ing clue what I
    was thinking of!
     
    red floyd, Jul 12, 2005
    #11
  12. This kind of thing is done all the time. I learned this trick from
    Stingray code.
    Here's a made up example:

    SetFont(MyFontClass().Bold()
    .Underlined());

    The SetFont() routine takes a const reference.
    (You can't use this if you are in one of those companies that doesn't
    allow passing anything by reference or doesn't allow returning a
    reference)

    Stuart
     
    Stuart MacMartin, Jul 12, 2005
    #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. Manu
    Replies:
    1
    Views:
    6,136
    Natty Gur
    Jul 20, 2003
  2. Kelvin Moss

    Is this legal stuff as per C Standard?

    Kelvin Moss, Oct 28, 2004, in forum: C Programming
    Replies:
    10
    Views:
    500
    Thomas Stegen
    Oct 29, 2004
  3. z-man
    Replies:
    11
    Views:
    482
    Tom Hawtin
    Apr 23, 2007
  4. Pallav singh

    is this code legal w.r.t. C standard ?

    Pallav singh, Oct 28, 2010, in forum: C Programming
    Replies:
    9
    Views:
    404
    Seebs
    Oct 28, 2010
  5. Matthew Heidemann
    Replies:
    14
    Views:
    258
    eT Ma
    Mar 22, 2011
Loading...

Share This Page