By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

current standard is it legal?

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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).
Jul 23 '05 #2

P: n/a
"red floyd" <no*****@here.dude> wrote in message
news:VE*****************@newssvr13.news.prodigy.co m
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

Jul 23 '05 #3

P: n/a
In article <11**********************@f14g2000cwb.googlegroups .com>,
puzzlecracker <ir*********@gmail.com> 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
em****@soda.CSUA.Berkeley.EDU
Jul 23 '05 #4

P: n/a
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

Jul 23 '05 #5

P: n/a
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!

Jul 23 '05 #6

P: n/a
"puzzlecracker" <ir*********@gmail.com> 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.
Jul 23 '05 #7

P: n/a
Torsten Mueller wrote:
"puzzlecracker" <ir*********@gmail.com> 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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Jul 23 '05 #8

P: n/a
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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Jul 23 '05 #9

P: n/a
Dietmar Kuehl <di***********@yahoo.com> 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.
Jul 23 '05 #10

P: n/a
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!

Jul 23 '05 #11

P: n/a
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

Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.