为什么隐式生成的构造函数(等)比用户定义的(微不足道的)更高效? [英] Why would the implicitly generated constructor (et al.) be more efficient than a user-defined (trivial) one?

查看:155
本文介绍了为什么隐式生成的构造函数(等)比用户定义的(微不足道的)更高效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了这个删除的函数,并且不能理解关于性能的部分,即:


特殊成员函数的手动定义琐碎)通常比隐式定义的效率低。


通过googling找到答案,我发现了另一个条作者:


合成的构造函数和复制构造函数使得实现可以创建比用户编写的代码更高效的代码,因为它可以应用不总是可能的优化。


没有解释,但我读了时间来处理类似的声明。



但是怎么写:

  class C {C()= default; }; 

可以比

更有效

  class C {C(){}}; 

?我虽然编译器会足够聪明,以检测这种情况并优化。换句话说,当它看到 = default 而不是 {}时,编译器更容易优化函数)?



编辑:编辑问题以添加c ++ 11标记,但此问题保留在c ++ 03上下文中: c $ c> class C {C()= default;}; 通过 class C {}; ,所以不是真的一个c ++ 11

解决方案

您问,这是怎么回事?

  class C {C()= default; }; 

可以比

更有效

  class C {C(){}}; 

好吧,两个构造函数什么都不做,所以谈论效率的例子是没有意义的。



但更一般地,在eg一个复制构造函数可以想象一次复制一个POD项目不会被简单优化器识别为可优化,而对于自动生成它可能只是做一个 memcpy 。谁知道。这是一个执行质量问题,我可以很容易地想象对面



干杯& hth。,


I read this article from D. Kalev this morning about the new c++11 feature "defaulted and deleted functions", and can't understand the part about performance, namely:

the manual definition of a special member function (even if it's trivial) is usually less efficient than an implicitly-defined one.

By googling to find an answer, I found another article of the same author:

the synthesized constructor and copy constructor enable the implementation to create code that's more efficient than user-written code, because it can apply optimizations that aren't always possible otherwise.

There is no explication, but I read time to time similar claims.

But how is it that writing:

class C { C() = default; };

can be more efficient than

class C { C(){} };

? I though a compiler would be smart enough to detect such situation and optimize that. In other words how is it easier for the compiler to optimize when it sees =default instead of {} (void body function)?

Edit: the question was edited to add the "c++11" tag, but this question remains in c++03 context: just replace class C {C()=default;}; by class C {};, so not really a c++11 specific question.

解决方案

You ask, how is it that

class C { C() = default; };

can be more efficient than

class C { C(){} };

Well, both constructors do nothing, so it's meaningless to talk about efficiency for that example.

But more generally, in e.g. a copy constructor one can imagine that copying one POD item at a time will not be recognized as optimizable by simple optimizer, whereas with automatic generation it might just do a memcpy. Who knows. It's a Quality of Implementation issue, and I can easily imagine also the opposite.

So, measure, if it matters.

Cheers & hth.,

这篇关于为什么隐式生成的构造函数(等)比用户定义的(微不足道的)更高效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆