From: itaj sherman on

std::string output( std::string const& r )
{
std::string const b( "(" + r + ")" );
std::cout << b << "; ";
return b;
}

template< typename ... ri >
nop( ri const& ... r )
{}


the order of evaluation of parameters is unspecified, so the following
expression:
nop( output( output("1") ), output( "2" ) );

can be evaluated in one the following orders (the std::cout output
would be one of the lines):

(1); ((1)); (2);
(2); (1); ((1));
(1); (2); ((1));


would the following make any difference?

nop( ( true ? output( output("1") ) : std::string() ),
output( "2" ) );

is the third order still allowed?

itaj


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 23 Jul., 02:00, itaj sherman <itajsher...(a)gmail.com> wrote:
> std::string output( std::string const& r )
> {
> std::string const b( "(" + r + ")" );
> std::cout << b << "; ";
> return b;
> }
>
> template< typename ... ri >
> nop( ri const& ... r )
> {}
>
> the order of evaluation of parameters is unspecified, so the following
> expression:
> nop( output( output("1") ), output( "2" ) );
>
> can be evaluated in one the following orders (the std::cout output
> would be one of the lines):
>
> (1); ((1)); (2);
> (2); (1); ((1));
> (1); (2); ((1));

Correct.

> would the following make any difference?
>
> nop( ( true ? output( output("1") ) : std::string() ),
> output( "2" ) );
>
> is the third order still allowed?

I think that the third order is still supported, because
it does not contradict the requirements of the conditional
operator. A possible order could be:

Decide to evaluate first argument first, that is start with
evaluating the conditional operator:

1) Evaluate first operand => true
2) Evaluate top-level expression of second operand
(output("1"))
3) Evaluate second function argument (output( "2" ))
4) Evaluate second-level expression of the second
operand of the conditional operator from the first
argument.

This should still satisfy the order-requirements
of the conditional operator.

Additional to the questionable code-style
recommendation to (ab)use the conditional operator
to enforce the evaluation order, two better
recommendations are:

a) Just accept the current C++ state and write with
the appropriate amount of gnashing of teeth:

std::string first(output( output("1") ));
nop( first, output( "2" ) );

b) Proceed complaining about the current C++
state and require that function arguments are
evaluated in a specified order - I'm not kidding!
(Btw.: Good morning, Hyman Rosen ;-))

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]