short circuited expressions...

S

Steven Wong

Just wondering, is it portable to write:

something* pSomething = getSomething();

if( pSomething && pSomething->stuff() ) {

....

}

I'm not sure if the condition statement is always shortcircuited for
every implementation of C++?

TIA
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Steven Wong escribió:
I'm not sure if the condition statement is always shortcircuited for
every implementation of C++?

It is. Your C++ manual don't say that? Find another.

Regards.
 
M

Mike Wahler

Steven Wong said:
Just wondering, is it portable to write:

something* pSomething = getSomething();

if( pSomething && pSomething->stuff() ) {

...

}

I'm not sure if the condition statement is always shortcircuited for
every implementation of C++?

The language standard dicates that it must. However, there's
no possible way it can actually enforce this rule. An implementation
that does not obey it is not conforming, and imo should not be used.

-Mike
 
J

Jerry Coffin

gndm_2001 said:
Just wondering, is it portable to write:

something* pSomething = getSomething();

if( pSomething && pSomething->stuff() ) {

...

}

I'm not sure if the condition statement is always shortcircuited for
every implementation of C++?

Yes it is, UNLESS you've overloaded operator&& for the types involved.

If you have something that doesn't short-circuit evaluations like this,
it's not C++ and not really even much like C++ (or C) either -- if I'm
not mistaken, short circuit evaluation has been been part of C since
before the beginning (i.e. back at least as far as B, and quite possibly
to BCPL or even CPL).
 

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
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top