Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > current standard is it legal?

Reply
Thread Tools

current standard is it legal?

 
 
puzzlecracker
Guest
Posts: n/a
 
      07-12-2005
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.

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      07-12-2005
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).
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      07-12-2005
"red floyd" <(E-Mail Removed)> wrote in message
news:VEEAe.1149$(E-Mail Removed) 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

 
Reply With Quote
 
E. Mark Ping
Guest
Posts: n/a
 
      07-12-2005
In article <(E-Mail Removed) .com>,
puzzlecracker <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
sandSpiderX
Guest
Posts: n/a
 
      07-12-2005
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

 
Reply With Quote
 
Wallking
Guest
Posts: n/a
 
      07-12-2005
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!

 
Reply With Quote
 
Torsten Mueller
Guest
Posts: n/a
 
      07-12-2005
"puzzlecracker" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      07-12-2005
Torsten Mueller wrote:
> "puzzlecracker" <(E-Mail Removed)> 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...
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      07-12-2005
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.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
 
Reply With Quote
 
Torsten Mueller
Guest
Posts: n/a
 
      07-12-2005
Dietmar Kuehl <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using the current method name within current method Matthew Heidemann Ruby 14 03-22-2011 03:56 AM
printing out the current URL for current ASP page in Perl Jack Perl Misc 3 12-22-2008 02:35 PM
Thread.current.parent and Thread.current.ancestors ara.t.howard@noaa.gov Ruby 3 02-16-2007 03:34 PM
How to update the current web page? (delete current web page + rewrite the page) laryten@yahoo.com Javascript 3 10-12-2006 07:33 PM
How to change the current thread current culture at run time. Manu ASP .Net 1 07-20-2003 03:27 PM



Advertisments