内联限定词源自原型或定义? [英] Inline qualifier stems from prototype or definition?
问题描述
我不太确定这在标准。假设我有三个档案,例如:
I'm not quite sure about this in the standards. Say I have three files like this:
foo.h
#include <iostream>
inline void foo();
void foo()
{
std::cout << "Foo" << std::endl;
}
foo.cpp: b
$ b
foo.cpp:
#include "foo.h"
void baz();
int main()
{
baz();
foo();
}
bar.cpp
$ b
bar.cpp
#include "foo.h"
void baz()
{
foo();
}
现在,foo的定义将被编译成两个编译单元foo.o和bar.o.如果我理解它正确,内联函数将避免链接器collition。 G ++编译和链接这只是很好,但与clang ++ 2.8我得到这个错误:
Now, the definition for foo will be compiled into both compilation units foo.o and bar.o. If I understand it correctly, having inlined functions will avoid linker collition. G++ compiled and links this just fine, but with clang++ 2.8 I get this error:
/tmp/cc-7RdmYP.o: In function `foo()':
bar.cpp:(.text+0x50): multiple definition of `foo()'
/tmp/cc-LW3id3.o:foo.cpp:(.text+0x50): first defined here
collect2: ld returned 1 exit status
clang ++看不到 void foo()
作为内联函数。然而,它确实工作正常,当我添加内联的定义以及。
It seems that clang++ does not see void foo()
as an inlined function. It does however, work fine when I add inline to the definition as well.
我必须在 void foo()
中添加内联,以便将其视为内联函数,或者是一个clang ++错误?
Do I have to add inline to void foo()
as well here to have it be seen as an inlined function, or is this a clang++ bug?
推荐答案
C ++ 0X draft N3225在 2函数说明符
:
C++0X draft N3225 says in 7.1.2 Function specifiers
:
-
语句2:内联函数
-
第4条:内联函数应在其使用的每个转换单元中定义,完全相同的定义在每种情况下。
clause 2: A function declaration with an inline specifier declares an inline function
clause 4: An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case.
; clang错误,但仍有一个(slim)的机会,在C ++ 03中的东西是不同的。
So, to me, it looks like gcc is right & clang wrong, but there's still a (slim) chance that things were (are?) different in C++03..
这篇关于内联限定词源自原型或定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!