Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Evaluation order of initializer list parameters (http://www.velocityreviews.com/forums/t806032-evaluation-order-of-initializer-list-parameters.html)

Juha Nieminen 11-17-2011 05:16 PM

Evaluation order of initializer list parameters
 
IIRC for example when calling a function taking several parameters,
the order in which those parameters will be evaluated is not specified
by the standard. (In other words, if you write "f(a, b)" then b might
be evaluated before a.) This means that if I do this:

f(func1(), func2());

it's not guaranteed that func1() will be called first and func2() after
that. It may happen the other way around.

Is this also so with initializer lists? For example, if I initialize
a struct like this:

SomeStruct obj = { func1(), func2() };

is it guaranteed that func1() will be called first, and then func2()?
(I know that the elements of the struct will be initialized in order.
However, are the parameters to those elements also evaluated in that
same order?)

Likewise for an array:

int anArray[] = { func1(), func2() };

is it guaranteed that func1() will be called first, and then func2()?
(Also here the array elements are initialized in order, but does that
mean that the parameters are evaluated in the same order?)

How about C++11 initializer lists? Such as:

std::vector<int> aVector = { func1(), func2() };

Victor Bazarov 11-17-2011 07:11 PM

Re: Evaluation order of initializer list parameters
 
On 11/17/2011 12:16 PM, Juha Nieminen wrote:
> IIRC for example when calling a function taking several parameters,
> the order in which those parameters will be evaluated is not specified
> by the standard.[..]
>
> Is this also so with initializer lists? [..]


Yes. The evaluation order is not explicitly specified.

V
--
I do not respond to top-posted replies, please don't ask

Johannes Schaub 11-19-2011 03:03 AM

Re: Evaluation order of initializer list parameters
 
Juha Nieminen wrote:

> IIRC for example when calling a function taking several parameters,
> the order in which those parameters will be evaluated is not specified
> by the standard. (In other words, if you write "f(a, b)" then b might
> be evaluated before a.) This means that if I do this:
>
> f(func1(), func2());
>
> it's not guaranteed that func1() will be called first and func2() after
> that. It may happen the other way around.
>
> Is this also so with initializer lists? For example, if I initialize
> a struct like this:
>
> SomeStruct obj = { func1(), func2() };
>


No, the order is specified to be from left to right, and each evaluation of
an element is sequenced after the evaluation of the preceeding element.

That applies disregarding of the semantics of the initializer list. For
example

struct A {
A(int, int);
};

int f();
int g();

A a = { f(), g() };

In this example, first f is called, and then g, even though both of these
function calls are arguments of a call to a constructor of A.


All times are GMT. The time now is 04:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57