用户定义的默认构造函数效率较低吗? [英] Are user-defined default constructors less efficient?
问题描述
几天前,在阅读标准C ++新闻时,我已经阅读了有关C++11中的默认函数,该文章提到用户-定义的构造函数比编译器生成的构造函数效率低:
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:
- 为什么用户默认的构造函数(或特殊的成员函数)比编译器隐式定义的效率低?
- 通过显式默认构造函数(或特殊成员函数)如何提高效率?
- 选择默认的构造函数(或特殊成员函数)时必须遵循哪些准则?效率如何影响此决定?
推荐答案
我认为更好的说法是,用户定义的默认构造函数 MAY 比生成的编译器效率低.
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.
这篇关于用户定义的默认构造函数效率较低吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!