473,407 Members | 2,359 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,407 software developers and data experts.

Is this defined?

Recently, I saw someone use this marco:

#define MAKE_STRING(msg) \
(((ostringstream&)(ostringstream()<<msg)).str())

(Assume that the std namespace had been brought into the global scope.)

My question is that if this is defined. I think isn't. It seems to me
that the temporary is being passed around to non-const references
during its lifetime. Am I just plain wrong?

Jul 23 '05 #1
3 1184
Razzer wrote:
Recently, I saw someone use this marco:

#define MAKE_STRING(msg) \
(((ostringstream&)(ostringstream()<<msg)).str())

(Assume that the std namespace had been brought into the global scope.)

My question is that if this is defined. I think isn't. It seems to me
that the temporary is being passed around to non-const references
during its lifetime. Am I just plain wrong?


I really don't see a problem with that code.
Basically it's the same as if you write something like

int x = atoi( (std::string("1") + std::string("2")).c_str() );

which should be perfectly fine.

Temporaries created as a result of expression evaluation are
guaranteed to live exactly until the end of the expression
statement, which would be that ";" thingy.

Meanwhile it's perfectly fine to use them as an l-value like in:

int x = atoi( (std::string() = "10").c_str() );

or even

int foo = atoi( ((std::string() = "10") = "142").c_str() + 1 );

or like in the MAKE_STRING macro you posted.

I think what you mean is the nasty

std::string& foo = std::string("foo");

thingy which is of course bullshit (although it compiles).

But since the "str()" function (which is the "result" of that
MAKE_STRING macro you posted) finally returns a "basic_string"
and not a referenct to something that's all not a problem here.

And finally, the cast to "ostringstream&" is only there because
operator "<<" returns a ref to "basic_ostream" (base-type), but
the "str()" function is defined in "ostringstream" so you have
to cast it back to a ref to the "real" type to be able to call it.
Jul 23 '05 #2
On Fri, 17 Jun 2005 03:31:59 +0400, Razzer <co********@gmail.com> wrote:
Recently, I saw someone use this marco:

#define MAKE_STRING(msg) \
(((ostringstream&)(ostringstream()<<msg)).str())

(Assume that the std namespace had been brought into the global scope.)

My question is that if this is defined. I think isn't. It seems to me
that the temporary is being passed around to non-const references
during its lifetime. Am I just plain wrong?


Well formedness of this macro depends on the type of msg and the
overloaded operator<< for std::ostream& and msg. If it is a standard
std::ostream& std::operator<<(std::ostream&, ...) then the code is
ill-formed since an r-value (the temporary created as ostringstream()) can
not be bound to a reference to a non-constant. If it is an overloaded
operator<<, which takes an std::ostream/ostringstream as a reference to
constant and returns something that can be casted to std::ostringstream&,
then the code might be well-formed.

--
Maxim Yegorushkin
Jul 23 '05 #3
Meanwhile it's perfectly fine to use them as an l-value like in:

int x = atoi( (std::string() = "10").c_str() );

or even

int foo = atoi( ((std::string() = "10") = "142").c_str() + 1 );


Argh. Sorry, just forget that. It's not using the temporary as an
l-value, since it's just calling a member function which is not
forbidden on non-l-values.
Of course the temporary _is_ a non-l-value ... this stuff always
confuses me a little - I should have kept my great ideas to myself.

Sorry again.

Nevertheless I think the MAKE_STRING macro will work, but if it's
really 100% O.K. with the standard I can't tell.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dave | last post by:
Hello all, Please consider this code: #ifndef FOO_INCLUDED #define FOO_INCLUDED // File: foo.h class foo {
2
by: David Emme | last post by:
Access 97 I have a number of SELECT statements which contain references to user-defined VBA functions. These typically work as expected, but occasionally, on one user's machine or another,...
3
by: Leiji Liu | last post by:
Hi, I am curious if there are any pre-defined varables (constants?) in C? I saw some code with __LINE__, __FILE__, etc. Are those located in some include files? LL
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
7
by: William L. Bahn | last post by:
I'm working on some lessons and want to be sure I get some stuff correct. If any of this is covered in the FAQ, please feel free to point me to the section - I couldn't find it. ...
6
by: Michael B Allen | last post by:
Which is the preferred method for preprocessor tests and why? #ifdef XYZ or #if XYZ or #if defined(XYZ) and
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
1
by: sethuganesh | last post by:
HI, i have ported vc++ 6.0 code to visual studio 2005. During batch build in debug mode i din't get any error.But if i build the same in release mode i am getting the following error. ...
2
by: curious2007 | last post by:
During the linking I get the following: 1>Linking... 1>main.obj : error LNK2005: "double __cdecl sigma(class curious2007::pair<double,double> const &)" (?sigma@@YANABV?$pair@NN@curious2007@@@Z)...
7
by: skillzero | last post by:
Is there a way to cause a compile/preprocessor error in a macro if a preprocessor symbol is not defined? I want to do the equivalent of this in a macro: #if( !defined( SOME_FLAG ) ) #error not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.