q****x 发帖数: 7404 | 1 【 以下文字转载自 JobHunting 讨论区 】
发信人: quantx (X矿工), 信区: JobHunting
标 题: Re: c++ inline问题
发信站: BBS 未名空间站 (Tue Dec 6 01:53:26 2011, 美东)
下面这个例子,即使没有inline关键字,X::f()也必须inline,否则ODR违例?
这里的inline语法上必须有,但编译器还是可以处理成普通函数,还是说必须inline?
// class_X.h
#include
using namespace std;
class X {
public:
void f();
};
void X::f()
{
cout << "X::f()" << endl;
}
//class_X1.cpp
#include "class_X.h"
void f()
{
X x;
x.f();
}
// class_X2.cpp
#include "class_X.h"
int main()
{
X x;
x.f();
f();
}
g++ class_X1.cpp class_X2.cpp |
X****r 发帖数: 3557 | 2 任何地方的inline都只是一个提示,编译器可以inline也可以不inline。
【在 q****x 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: quantx (X矿工), 信区: JobHunting : 标 题: Re: c++ inline问题 : 发信站: BBS 未名空间站 (Tue Dec 6 01:53:26 2011, 美东) : 下面这个例子,即使没有inline关键字,X::f()也必须inline,否则ODR违例? : 这里的inline语法上必须有,但编译器还是可以处理成普通函数,还是说必须inline? : // class_X.h : #include : using namespace std; : class X {
|
q****x 发帖数: 7404 | 3 这里inline是必须加的,怎么是提示?提示你完全可以不加啊。
【在 X****r 的大作中提到】 : 任何地方的inline都只是一个提示,编译器可以inline也可以不inline。
|
q****x 发帖数: 7404 | 4 再说如果是否内联由编译决定,那直接取消这个关键字不就行了?它存在的意义何在?
你不加,编译器也可能内联。你加了,编译器也可能不内联。
【在 q****x 的大作中提到】 : 这里inline是必须加的,怎么是提示?提示你完全可以不加啊。
|
t****t 发帖数: 6806 | 5 你必须加, 但是对编译器只是提示.
【在 q****x 的大作中提到】 : 这里inline是必须加的,怎么是提示?提示你完全可以不加啊。
|
X****r 发帖数: 3557 | 6 inline关键字两个作用:
3.2 One definition rule
5 There can be more than one definition of ..., inline function
with external linkage, ...
7.1.2 Function specifiers
2 A function declaration with an inline specifier declares an
inline function. The inline specifier indicates to the
implementation that inline substitution of the function body
at the point of call is to be preferred to the usual function
call mechanism. An implementation is not required to perform
this inline substitution at the point of call; however,
even if this inline substitution is omitted, the other rules
for inline functions defined by this subclause shall still be
respected.
所以inline关键字对于函数inline替换效果的作用只是一个提示,没有强制作用。
【在 q****x 的大作中提到】 : 这里inline是必须加的,怎么是提示?提示你完全可以不加啊。
|
q****x 发帖数: 7404 | 7 如果我加了,编译器忽略,那这个函数会放哪个.o文件里?编译器任选一个?
另外模板是不是也有类似情况?比如定义一个递归的函数模板,编译器怎么处理?
【在 t****t 的大作中提到】 : 你必须加, 但是对编译器只是提示.
|
t****t 发帖数: 6806 | 8 most likely it's in multiple files, but link editor will handle this (google
"weak symbol").
【在 q****x 的大作中提到】 : 如果我加了,编译器忽略,那这个函数会放哪个.o文件里?编译器任选一个? : 另外模板是不是也有类似情况?比如定义一个递归的函数模板,编译器怎么处理?
|
q****x 发帖数: 7404 | 9 i searched "weak symbol" but don't see how it connects to this multiple
definition issue...
google
【在 t****t 的大作中提到】 : most likely it's in multiple files, but link editor will handle this (google : "weak symbol").
|
t****t 发帖数: 6806 | 10 ok i remembered wrong, weak symbol is not exactly for this (but it can be
used to implement this). just remember template/inline member are special
cases, multiple definitions are accepted and handled by link editor.
【在 q****x 的大作中提到】 : i searched "weak symbol" but don't see how it connects to this multiple : definition issue... : : google
|
|
|
a****h 发帖数: 126 | 11 你的code里没有 inline keyword, 跟inline 有什么关系?
哪位大侠给菜鸟讲解一下。
【在 q****x 的大作中提到】 : i searched "weak symbol" but don't see how it connects to this multiple : definition issue... : : google
|
r****t 发帖数: 10904 | 12 不光是这个,header 里面不应该用 using.
【在 q****x 的大作中提到】 : i searched "weak symbol" but don't see how it connects to this multiple : definition issue... : : google
|
p*********t 发帖数: 2690 | 13 compiler决定用不用inline.所以,尽量少用.
【在 q****x 的大作中提到】 : i searched "weak symbol" but don't see how it connects to this multiple : definition issue... : : google
|
f******y 发帖数: 2971 | 14 inline is a hint to compiler, so it is not totally useless.
【在 p*********t 的大作中提到】 : compiler决定用不用inline.所以,尽量少用.
|
r****t 发帖数: 10904 | 15 http://www.mitbbs.com/article_t/Programming/31204085.html
【在 q****x 的大作中提到】 : i searched "weak symbol" but don't see how it connects to this multiple : definition issue... : : google
|
e*******c 发帖数: 804 | 16 agree
X::f is defined outside of class but in the header file. The header file is
included in two files, so each will have a copy of X::f definition, and
hence redefinition.
You should either move X::f definition inside class (so the function is
inline, and will have internal linkage), or move it to the implementation
file for class X.
Put function definition in a header file is really bad habit. Also, "using
std" in a header file is the source of a lot of trouble.
【在 r****t 的大作中提到】 : 不光是这个,header 里面不应该用 using.
|