内联构造函数 [英] Inlining Constructors

查看:151
本文介绍了内联构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我记得从某个地方读到内联构造函数是一个

''BadThing'',但现在似乎无法找到原始来源。我不能说b $ b然而这对于简单的构造者来说是个原因...

任何想法

Mike

解决方案

5月24日下午1:52,Michael Hull< mikehul ... @ googlemail.comwrote:




我记得从某个地方读到内联构造函数是一个

''BadThing'',但现在不能似乎找到了原始来源。我不能说b $ b然而这对于简单的构造者来说是个原因...

任何想法


Mike





对于具有隐式数据类型的类的简单构造函数为

数据成员只有它可以使用内联构造函数,但即使它是

也没有得到。


但对于繁重的类(具有许多用户定义数据类型的类为

数据成员)内联构造函数是一件坏事,如果该类继承自其他类,它会变得更糟糕。


当您查看构造函数中的行数时,您将认为应该允许它,但是对象构造会导致

调用这么多构造函数调用(所有组合对象和所有

基类及其组合对象)。


如果我错了请纠正我。


Vineet


Vineet Chawla写道:


5月24日下午1:52,Michael Hull< mikehul ... @ googlemail.comwrote :


>
我记得从某个地方读到内联构造函数是''BadThing'',但现在可以''似乎找到了原始来源。然而,我无法理解为什么它会成为简单的构造者...
任何想法

Mike




对于具有隐式数据类型的类的简单构造函数作为其

数据成员,只有内联构造函数可以,但即使它是

没有派生。


但对于重类(具有许多用户定义数据类型的类作为

数据成员)内联构造函数是如果这个类继承自其他类,那么它会变得更糟糕。


当你看看构造函数中的行数时你会觉得它应该被允许,但是一个对象构造导致

调用这么多构造函数调用(所有组合对象和所有元素) b基类及其组合对象)。



你在描述它是坏的,但不是为什么它是坏的,所以这不是

帮助。我可以想到几个原因:内联大型函数

会导致大量代码膨胀,从而增加可执行文件的大小

并且可能会降低性能。另一个原因可能是构造函数越大

,它对其余代码的依赖性(平均而言)就越多了,导致你重建大部分代码你的

应用程序,即使是很小的变化,也不需要对构造函数的用户使用




但是,这些原因同样适用于常规函数,并且

threadstarter明确地讨论了构造函数。除此之外,

请记住''inline''只是对编译器的一个提示 - 一个好的
编译器会对是否内联函数做出自己的判断

与否,无论构造函数/函数是否实际定义为

是内联的,所以第一个参数非常没用。


总而言之,我不知道为什么特别是内置构造函数

是一件坏事。


- 西尔维斯特


Sylvester Hesp写道:


除此之外,

请记住''inline''只是对编译器的提示



这不是*仅*一个提示。它不止于此(特别是与链接相关的
)。


Hi,
I remember from somewhere reading that inlining constructors is a
''BadThing'', but now can''t seem to find the original source. I can''t
however thing of a reason why it would be for simple constructors...
any thoughts
Mike

解决方案

On May 24, 1:52 pm, Michael Hull <mikehul...@googlemail.comwrote:

Hi,
I remember from somewhere reading that inlining constructors is a
''BadThing'', but now can''t seem to find the original source. I can''t
however thing of a reason why it would be for simple constructors...
any thoughts

Mike

Hi,
For simple constructors of classes having implicit data types as their
data members only it is ok to have inline constructors but even it is
not getting derived.

But for heavy classes (classes with many user defined data types as
data members) inlining constructors is a bad thing and it becomes even
more worse if the class is inherited from some other class.

When you will look at the number of lines in the constructor you will
feel it should be allowed, but an object construction results in
calling so many constructor calls (of all composed objects and of all
base classes and their composed objects).

Please correct me if i am wrong.

Vineet


Vineet Chawla wrote :

On May 24, 1:52 pm, Michael Hull <mikehul...@googlemail.comwrote:

>Hi,
I remember from somewhere reading that inlining constructors is a
''BadThing'', but now can''t seem to find the original source. I can''t
however thing of a reason why it would be for simple constructors...
any thoughts

Mike


Hi,
For simple constructors of classes having implicit data types as their
data members only it is ok to have inline constructors but even it is
not getting derived.

But for heavy classes (classes with many user defined data types as
data members) inlining constructors is a bad thing and it becomes even
more worse if the class is inherited from some other class.

When you will look at the number of lines in the constructor you will
feel it should be allowed, but an object construction results in
calling so many constructor calls (of all composed objects and of all
base classes and their composed objects).

You''re describing WHEN it is bad, but not WHY it is bad, so this is not
helping. I can think of several reasons: inlining large functions
causes massive code bloat, thus increasing the size of your executable
and probably decreasing performance. Another reason might be that the
larger the constructor is, the more dependencies (on average) it has on
the rest of the code, causing you to rebuild large parts of your
application for even small changes that don''t have to do anything with
the users of the constructor in question.

However, these reasons all apply to regular functions as well, and the
threadstarter was explicitely talking about constructors. Besides that,
keep in mind that ''inline'' is only a hint to the compiler - a good
compiler will make his own judgements on whether to inline a function
or not, no matter if the constructor/function was actually defined as
being inline, so the first argument is pretty moot.

So to conclude, I have no idea why inlining constructors in particular
is a Bad Thing.

- Sylvester


Sylvester Hesp wrote:

Besides that,
keep in mind that ''inline'' is only a hint to the compiler

It''s not *only* a hint. It does more than that (specifically
related to linking).


这篇关于内联构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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