[英]Are member functions defined in a class definition compiled differently than member functions defined elsewhere in C++?
Are member functions defined in a class definition compiled differently than member functions defined elsewhere in C++? 在类定义中定义的成员函数是否与在C ++中其他地方定义的成员函数的编译方式不同? For example, consider the following foo.h
例如,考虑以下foo.h
#pragma once
struct foo {
void bar() {}
void buz();
};
and foo.cpp 和foo.cpp
#include "foo.h"
void foo::buz() {};
If we look at the symbols for foo.o 如果我们看一下foo.o的符号
$ g++ -c foo.cpp
$ nm -a foo.o
0000000000000000 b .bss
0000000000000000 n .comment
0000000000000000 d .data
0000000000000000 r .eh_frame
0000000000000000 a foo.cpp
0000000000000000 n .note.GNU-stack
0000000000000000 t .text
0000000000000000 T _ZN3foo3buzEv
$ c++filt _ZN3foo3buzEv
foo::buz()
we see that we only have a symbol for foo::buz
. 我们看到我们只有
foo::buz
的符号。 Now, say that we compile multiple files that all include foo.h
and then create a library from the result. 现在,假设我们编译了多个包含
foo.h
文件,然后从结果中创建一个库。 Are the member functions bar
and buz
treated differently? 成员函数
bar
和buz
是否区别对待?
Yes, there is a difference. 是,有一点不同。 If a member function is defined inside class definition then the compiler tries to make it an inline function.
如果在类定义中定义了成员函数,则编译器会尝试使其成为内联函数。 For your example, the function is simple enough to make it inline.
对于您的示例,该函数非常简单,可以使其内联。 So the compiler has made
bar
inline and you only see symbol of baz
. 所以编译
baz
bar
内联,你只看到baz
符号。
Whether it will be good or bad depends largely on the specific functions and your use case. 它的好坏在很大程度上取决于具体的功能和用例。 Inline function do not need an actual function call, the there is a performance improvement there.
内联函数不需要实际的函数调用,那里有性能改进。 But the downside is if you include the class header in many places then that will increase the binary size.
但缺点是如果在许多地方包含类标题,那么将增加二进制文件大小。
Also note that inline is a request to the compiler. 另请注意,inline是对编译器的请求。 The compiler is free to ignore the request and treat it a normal method.
编译器可以自由地忽略请求并将其视为常规方法。
As from 9.2.1/1 : 从9.2.1 / 1开始 :
A member function may be defined in its class definition, in which case it is an inline member function
可以在其类定义中定义成员函数,在这种情况下,它是内联成员函数
On the other side, from 9.2.1/2 : 另一方面,从9.2.1 / 2 :
An inline member function (whether static or non-static) may also be defined outside of its class definition provided either its declaration in the class definition or its definition outside of the class definition declares the function as inline orconstexpr.
内联成员函数(无论是静态的还是非静态的)也可以在其类定义之外定义,前提是它在类定义中的声明或在类定义之外的定义将函数声明为内联orconstexpr。
The question was: Are member functions defined in a class definition compiled differently than member functions defined elsewhere in C++? 问题是: 类定义中定义的成员函数是否与C ++中其他地方定义的成员函数进行了不同的编译?
It mostly depends on how you define them, as you can deduce from the citations above. 它主要取决于您如何定义它们,因为您可以从上面的引文中推断出来。
In your example, they are actually different. 在您的示例中,它们实际上是不同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.