C++ inline functions: declare as such, define as such, or both? Why?

大憨熊 提交于 2019-12-12 08:05:34

问题


The following code segment compiles with no problems, even though foo is defined inline but not declared as such, and bar is declared inline but not defined as such.

int foo();
inline int foo() { return 3; }

inline int bar();
int bar() { return 4; }

inline int foobar();
inline int foobar() { return 5; }

int main(){
    // ...
}

My first question: does the compiler read foo as inline or not? What about bar? Is this specified by the C++ standard?

My second question: Which one of these is the best practice in declaring and defining inline functions? Is it foo? bar? or foobar? Why?


inb4 I read some other posts related to this but none of them answer my question directly.

This answer seems to suggest that foo is inline, but says nothing about bar. It also doesn't explain why foo is preferred over the others. This answer talks about when I should use inline functions. That's not my concern: I've already decided to use inline functions. My question (question 2, to be precise) is whether I should declare it as such, define it as such, or both, and why one of the conventions is better style than the rest. This question seems to be closer to my concern but nobody answered it.


回答1:


True for member functions, and not explicitly-defined for non-member functions (I believe)

See §10.1.6 in ISO C++ std.

The inline specifier can be applied only to the declaration or definition of a variable or function

and

A function declaration (11.3.5, 12.2.1, 14.3) with an inline specifier declares an inline function.

It doesn't explictly state what will happen if an inline specifier only modifies the definition of a function.

What we can be sure of is that such member functions are guaranteed to be marked as inline (thanks to James Curran).

See §12.2.1.

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 or constexpr.

All three functions in GCC and non-member circumstance

As in GCC -O1 C++ mode, every function mentioned are inlined.

Code:

#include "stdio.h"

int foo();
inline int foo() {int i; for(i=0;i<100000;i++); return i+3; }

inline int bar();
int bar() {int i; for(i=0;i<100000;i++); return i+4; }

inline int foobar();
inline int foobar() {int i; for(i=0;i<100000;i++); return i+5; }

int foobar2();
int foobar2() {int i; for(i=0;i<100000;i++); return i+6; }

int main(){
    int a,b,c,d;
    a=foo();
    b=bar();
    c=foobar();
    d=foobar2();
    printf("%d %d %d %d", a, b, c, d);
}

Disassembly:

We can see only foobar2 is called.

As in -O2 and -O3, inline doesn't matter so much. The compiler will decide by itself (in the case above, all 4 functions are inlined).




回答2:


(NOTE: I reopened this question, as this is NOT a duplicate of the cited older question. That question involved design; this is about syntax).

Now, referring to the question cited by the OP in his question about inline in definition and declaration, the answer states that if the declaration is in a header file, then it was have the "inline" ("bar style"), because other source files using that header will try to link to it as if it were a non-inlined function and file.

Personally, I'd use

 inline int foobar() { return 5; }

in the header file without a separate declaration. (I always feel that if it's too big to be in the header, then it's too big to be inlined.)



来源:https://stackoverflow.com/questions/44788412/c-inline-functions-declare-as-such-define-as-such-or-both-why

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!