473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

current standard is it legal?

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
11 1530
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
"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
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
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
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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Teddy | last post by:
Hello all simple code below: void foo() { } void foo(int i = 0) { } int main() { //foo(); // call of overloaded 'foo()' is ambiguous
6
by: The Directive | last post by:
Where can I get a free legal copy of the latest C standard. (ISO and ANSI organizations charge for it.) I have searched the web but failed to find it. This is the latest version? ISO/IEC...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
10
by: Kelvin Moss | last post by:
Hi group, In C++ it's undefined behavior if one tries to un-const the constness of a const variable with const_cast<>. I want to know if the same holds good in C too. E.g. const char *s =...
21
by: Kannan | last post by:
Its been a while I have done pure C programming (I was coding in C++). Is the following function valid according to standard C? int main(int argc, char *argv) { int x; x = 9; printf("Value...
5
by: kathleenmm | last post by:
It's too expensive for me to buy a copy of C++ 2003 standard. Are there any legally free copy available for download?
9
by: Alex | last post by:
Get the Name and Phone Number of the Current Windows User in a .NET Application I am writing a simple .NET (C#) application. It needs to "automatic" get the Name (last, first) and phone number...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
18
by: Stephan Beal | last post by:
Hi, all! Before i ask my question, i want to clarify that my question is not about the code i will show, but about what the C Standard says should happen. A week or so ago it occurred to me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.