为什么类成员函数内联? [英] Why are class member functions inlined?

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

问题描述

我想我之前已经问过我的问题,我已经阅读过,但仍然很困惑,因此要求澄清。



C ++标准说,类定义中定义的所有成员函数都是内联的



我也听说编译器可以忽略函数的内联。在上面的情况下是真的,如果在类定义的内部定义,它将总是内联的?



此外,这个设计背后的原因是什么,使所有的函数定义在类定义内部?内联与源文件和头文件有什么关系?



更新:因此,如果不内联,应该总是在类外定义他们的函数,对吗?



由JohnB更新2:在类定义中声明的两个函数永远不能调用对方,因为它们必须包含另一个函数的整体。在这种情况下会发生什么?(已经由Emilio Garavaglia回答)

解决方案

内联具有两种效果:


  1. 它告诉编译器函数代码可以扩展


  2. 它告诉编译器函数定义可以重复。

点1.是archaic,意思是编译器实际上可以做它喜欢为了优化代码。它将永远内联机器代码,如果它可以,并找到方便的做,如果不能,它永远不会这样做。



点2.是实际的意义term:如果你 define (指定主体)一个函数在头中,因为一个头可以包含在更多的源,你必须告诉编译器通知链接器



现在,通过语言规范,自由函数(未在类体中定义)默认不定义为内联,因此,在标题中定义类似

  void myfunc()
{}

如果标题包含在更多的源中,然后链接在同一个输出中,链接器将报告多重定义错误,因此需要定义

  inline void fn()
{}
pre>

对于类成员,默认是相反的:如果你只是声明它们,它们不会被内联。如果你定义它们,它们将是内联的。



所以标题应该像

  //头文件

class myclass
{
public:
void fn1()
{} //定义到类中,因此默认嵌入

void fn2();
};

inline void myclass :: fn2()
{} //在类外部定义,因此需要显式内联

如果 myclass :: fn2()定义到一个合适的源,必须松开 inline 关键字。


I think my question has been asked here before, I did read them but still little confused and therefore asking to make it clear.

The C++ standard says all member functions defined inside class definition are inline

I have also heard that compiler can ignore inlining of a function. Will that be true in the above case or it will be always inlined if defined inside class definition?

Also, what was the reason behind this design, making all functions defined inside class definition inline? And what inlining has to do with source and header files?

Update: So one should always define their functions outside class if not to be inlined, right?

Update 2 by JohnB: Two functions declared inside class definition could never call each other as they would have to each contain the whole body of the other function. What will happen in this case? (Already answered by Emilio Garavaglia)

解决方案

The confusion arise from the fact that the inline has two effects:

  1. It tells the compiler that the function code can be expanded where the function in called instead of effectively been called.
  2. It tells the compiler that the function definition can be repeated.

Point 1. is "archaic" in the sense that the compiler can in fact do what it likes in order to optimize code. It will always "inline" machine code if it can and find convenient to do and it will never do that if it cannot.

Point 2. is the actual meaning of the term: if you define (specify the body) a function in the header, since a header can be included in more sources, you must tell the compiler to inform the linker about the definition duplicates, so that they can be merged.

Now, by the language specification, free functions (not defined in class bodies) are by default not defined as inline, so defining in a header a thing like

void myfunc()
{}

if the header is included in more sources, then linked in a same output, the linker will report a multiple definition error, hence the need to define it as

inline void fn()
{}

For class members, the default is the opposite: if you just declare them, they will not be inlined. If you define them, they will be inline.

So a header should look like

//header file

class myclass
{
public:
    void fn1()
    {} //defined into the class, so inlined by default

    void fn2();
};

inline void myclass::fn2()
{} //defined outside the class, so explicit inline is needed

And if myclass::fn2() definition goes into a proper source, must loose the inline keyword.

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

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