Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Order of function calls when creating an object of a class

[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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading