2

[C++17]

I have a class:

class A
{
    int a;
    int b;
public:
    A(int a, int b) : a{ a }, b{ b } { }
};

and two functions:

int get_a() { return 1; }
int get_b() { return 2; }

Now I construct an object:

A a{ get_a(), get_b() };

The question: is it guaranteed for this case that the order of function evaluation is always get_a and then get_b?

Ragdoll Car
  • 141
  • 6
  • Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of related posts for this. – Jason Feb 09 '23 at 12:52

1 Answers1

4

This is called list-initialization.

From cppreference:

Every initializer clause is sequenced before any initializer clause that follows it in the braced-init-list. This is in contrast with the arguments of a function call expression, which are unsequenced(until C++17) indeterminately sequenced(since C++17).

Therefore, then answer is yes, get_a() will always be sequenced before get_b().

Fareanor
  • 5,900
  • 2
  • 11
  • 37