内联限定符源于原型或定义?

Mai*_*ter 11 c++ gcc clang

我对标准中的这一点不太确定.说我有三个这样的文件:

foo.h中

#include <iostream>

inline void foo();

void foo()
{
   std::cout << "Foo" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

Foo.cpp中:

#include "foo.h"

void baz();

int main()
{
   baz();
   foo();
}
Run Code Online (Sandbox Code Playgroud)

bar.cpp

#include "foo.h"

void baz()
{
   foo();
}
Run Code Online (Sandbox Code Playgroud)

现在,foo的定义将被编译为编译单元foo.o和bar.o. 如果我理解正确,内联函数将避免链接器冲突.G ++编译和链接这很好,但使用clang ++ 2.8我得到这个错误:

/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
Run Code Online (Sandbox Code Playgroud)

看来clang ++并不是void foo()一个内联函数.但是,当我向定义添加内联时,它确实正常工作.

我是否必须在void foo()此处添加内联以将其视为内联函数,或者这是一个clang ++错误?

Eug*_*nca 2

C++0X 草案 N3225 中说7.1.2 Function specifiers

  • 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.

所以,对我来说,看起来 gcc 是对的,clang 是错的,但是 C++03 中的情况仍然有(微小的)不同的可能性。