相关疑难解决方法(0)

我应该何时为函数/方法编写关键字'inline'?

我应该何时inline在C++中为函数/方法编写关键字?

看到一些答案,一些相关的问题:

  • 我应该何时写在C++函数/方法关键字"内联"?

  • 什么时候编译器不知道何时使函数/方法'内联'?

  • 当一个应用程序为函数/方法写入"内联"时,是否多线程是否重要?

c++ inline one-definition-rule

526
推荐指数
6
解决办法
12万
查看次数

gcc vs clang:使用-fPIC内联函数

考虑以下代码:

// foo.cxx
int last;

int next() {
  return ++last;
}

int index(int scale) {
  return next() << scale;
}
Run Code Online (Sandbox Code Playgroud)

使用gcc 7.2进行编译时:

$ g++ -std=c++11 -O3 -fPIC
Run Code Online (Sandbox Code Playgroud)

这会发出:

next():
    movq    last@GOTPCREL(%rip), %rdx
    movl    (%rdx), %eax
    addl    $1, %eax
    movl    %eax, (%rdx)
    ret
index(int):
    pushq   %rbx
    movl    %edi, %ebx
    call    next()@PLT    ## next() not inlined, call through PLT
    movl    %ebx, %ecx
    sall    %cl, %eax
    popq    %rbx
    ret
Run Code Online (Sandbox Code Playgroud)

但是,使用clang 3.9编译具有相同标志的相同代码时:

next():                               # @next()
    movq    last@GOTPCREL(%rip), %rcx
    movl    (%rcx), %eax
    incl    %eax
    movl …
Run Code Online (Sandbox Code Playgroud)

c++ gcc clang fpic

9
推荐指数
1
解决办法
1262
查看次数

标签 统计

c++ ×2

clang ×1

fpic ×1

gcc ×1

inline ×1

one-definition-rule ×1