11

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.

11
  • I think both break the One Definition Rule: en.cppreference.com/w/cpp/language/definition Commented Jun 27, 2017 at 19:35
  • A function as simple as { return 3; } will almost certainly be inlined, regardless of how you define/declare it. Commented Jun 27, 2017 at 19:35
  • 2
    @James It's a simple code snippet to demonstrate my question. Posting a "realistic" function would distract from my main point. Commented Jun 27, 2017 at 19:36
  • 1
    @Richard there are three functions defined: foo, bar, and foobar. Which two are breaking the one definition rule? (And how is it? I'm only defining the functions once.) Also please see my original questions. (first question and second question) Commented Jun 27, 2017 at 19:39
  • 1
    @RichardCritten - NO. Each function is defined only once. They have a possibly conflicting declaration, but only one definition. Commented Jun 27, 2017 at 19:39

2 Answers 2

2

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: IDA

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

Sign up to request clarification or add additional context in comments.

4 Comments

OK, good to know. It seems from your example that gcc inlines all 3.
Your answer is mostly useless. It is not interesting to know what is inlined by particular compiler with a particular set of options. The answer should contain quotes from the standard which explain the difference between all these declaration/definition pairs.
@EdgarRokyan thanks. I added related information from the standard.
No function is "guaranteed inlined." The best you can do is "guaranteed to be marked as available for inlining". The compiler, will make the final decision.
1

(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.)

3 Comments

Well, good point. I'd probably do that as well. It's just that I've seen places where people declare member functions as non-inline and later define them (in the header file) as inline, which makes me want to clear up this piece of syntax detail.
@HazySmoke Member functions are inline when defined in class body by default, as is required in C++ standards.
@Keyu Gan I meant like it's declared normally in the class. And then outside the class body, it's defined as inline. And it's defined in the header file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.