Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are member-initialization lists really more efficient?

I agree with the consensus that it's generally best to initialise C++ data members in a member initialization list rather than the body of a constructor, but I am sceptical of this explanation

The other (inefficient) way to build constructors is via assignment, such as: Fred::Fred() { x_ = whatever; }. In this case the expression whatever causes a separate, temporary object to be created, and this temporary object is passed into the x_ object’s assignment operator. Then that temporary object is destructed at the ;. That’s inefficient.

Is this actually correct? I would have expected the compiler to elide the default-constructed temporary object which is immediately replaced by assignment in the body. I don't know why I expected this but having read the above claim I guess I have been quietly assuming it for years.

Are member initialization lists actually more efficient? If so, is it for this reason?

like image 859
spraff Avatar asked Feb 15 '17 21:02

spraff


People also ask

What is the advantage of using member initializer list?

The most common benefit of doing this is improved performance. If the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed directly inside x_ — the compiler does not make a separate copy of the object.

Is initialization list faster?

Conclusion: All other things being equal, your code will run faster if you use initialization lists rather than assignment.

When must you use a member initializer list?

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.

Why should initializer lists be used rather than assigning member variables values in the constructor body?

Initialization lists allow you to choose which constructor is called and what arguments that constructor receives. If you have a reference or a const field, or if one of the classes used does not have a default constructor, you must use an initialization list.


Video Answer


1 Answers

In the words of Alexandrescu & Sutter (item 9) Don't pessimize prematurely

Avoiding premature optimization does not imply gratuitously hurting efficiency. By premature pessimization we mean writing such gratuitous potential inefficiencies as:

• Defining pass-by-value parameters when pass-by-reference is appropriate. (See Item25.)

• Using postfix + + when the prefix version is just as good. (See Item 28.)

• Using assignment inside constructors instead of the initializer list. (See Item 48.)

Whenever you are writing assignments inside constructors, your code reviewers will be on alert: is something special going on? Did he really want some special two-stage initialization (because there is an implicit default construction of the member being generated anyway!). Don't surprise the readers of your code gratuitously.

Note that Alexandrescu & Sutter go on in Item 48 to discuss the potential inefficiency, but don't claim anywhere that there an actual inefficiency in real optimized code. That is also beside the point, it's about expressing intent and avoiding the risk of inefficiency.

like image 134
TemplateRex Avatar answered Sep 29 '22 12:09

TemplateRex