Are user-defined default constructors less efficient?
Asked Answered
D

2

8

Some days ago, while reading Standard C++ news I've read the post about Defaulted functions in C++11, in that article is mentioned that the user-defined constructor is less efficient than the one generated by the compiler:

The user-defined default constructor is less efficient than the compiler implicitly defined default constructor.

Continuing the reading, there's an example where an user-defined constructor is marked as default, and then says:

the explicitly defaulted constructor is more efficient than a manually programmed default constructor.

I don't understand these assertions, so I was wondering:

  • Why a user-default constructor (or special member function) would be less efficient than the compiler implicitly defined one?
  • How is the efficiency improved by explicitly defaulting a constructor (or special member function)?
  • What guidelines I must follow to choose to default a constructor (or special member function) and how the efficiency affects this decision?
Disorient answered 5/6, 2013 at 13:33 Comment(2)
#4276361Shiri
Good one @Shiri but both questions doesn't have an accepted answer yet, even the one you mentioned that is from 2010! :ODisorient
C
4

I think a better statement is that a user-defined default constructor MAY be less efficient than a compiler generated out.

For example, when it's generating a default constructor internally the compiler may be able to make assumptions and optimizations that it can't make for a user-defined contstructor (side-effects come to mind).

Also keep in mind that a user-defined default constructor could do totally different work that default-constructing all its members, resulting in it being less efficient (but also more correct). This doesn't seem to be the case in the link you provided however.

Childish answered 5/6, 2013 at 13:39 Comment(1)
"Also keep in mind that a user-defined default constructor could do totally different work than default-constructing all its members" - Of course, but I guess that case is out of discussion anyway. But good point about possible larger-scale optimizations.Merchant
S
0

And we all know, that if it's written on internet then it is must be right... Wait, do we?

In the article where I found the first assertion of less efficient, the author tells the truth. Though you seem to misinterpret it -- in the example it refers to the hand-crafted ctor uses assignment. For no good reasons, and going against 2 decade old guidelines.

Next instance, same case. (As a practical note I shall add, that for any compiler claiming to have optimizations I expect the same assy output even for that form...)

I see no reason why the proper handwritten ctor would be different from the defaulted one in any ways, including efficiency. OTOH if they are identical why on earth write it? I'm all too happy that compiler makes it for me. And finally I can even control it in some ways I previously could not. Could use more of such functions. ;-)

Silicify answered 5/6, 2013 at 13:57 Comment(1)
"And we all know, that if it's written on internet then it is must be right" I've assumed that it must be right due to the font ;) that's why I mention the font.Disorient

© 2022 - 2024 — McMap. All rights reserved.