current standard is it legal?

P

puzzlecracker

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.
 
R

red floyd

puzzlecracker said:
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).
 
J

John Carson

red floyd said:
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.
 
E

E. Mark Ping

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.)
 
S

sandSpiderX

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
 
W

Wallking

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!
 
T

Torsten Mueller

puzzlecracker said:
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.
 
D

Dietmar Kuehl

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.
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.
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...
 
D

Dietmar Kuehl

red said:
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.
 
T

Torsten Mueller

Dietmar Kuehl said:
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.
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.
 
R

red floyd

Dietmar said:
red said:
[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!
 
S

Stuart MacMartin

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top