which inline-s are useless?

Which inline-s are useless, presuming, that all functions
actually must be inlined? Where is an inline missing?
(put in a header file, these all worked for me)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// function case
inline int iDecDefC() {return 1;}

inline int iDecDef();
inline int iDecDef() {return 10;}

inline int iDec();
       int iDec() {return 100;}

       int iDef();
inline int iDef() {return 1000;}


// method case
struct X
{
   inline int iDecDefC() {return 1;}
   inline int iDecDef();
   inline int iDec();
   int iDef();
};

inline int X::iDecDef() {return 10;}
int X::iDec() {return 100;}
inline int X::iDef() {return 1000;}
Well, then here I go:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// function case
inline int iDecDefC() {return 1;}

inline int iDecDef();
inline int iDecDef() {return 10;}

inline int iDec();
inline int iDec() {return 100;}

       int iDef();
inline int iDef() {return 1000;}


// method case
struct X
{
   inline int iDecDefC() {return 1;} // inline anyway
   inline int iDecDef();
   inline int iDec();
   int iDef();
};

inline int X::iDecDef() {return 10;}
inline int X::iDec() {return 100;}
inline int X::iDef() {return 1000;}


However, I don't know if that will be an inline function/method, if "inline" appears just in the declaration, I guess, no, maybe just in case of a method? (however, that's not very important)
In order to inline a function, I am petty sure you must have the entire body available.
Sure, it should be in a header file. I've seen projects #including inline functions header at the bottom of interface/declarations header.
One more idea: can it be, that when a LONG inline function is used into multiple translation units, compiler decides not to inline it and subsequently that function (previously #included everywhere) becomes defined in multiple places for linker?
To inline a function or not is the choice of a compiler. Inline keyword is just used as hint.

http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.7
1
2
3
4
5
6
7
8
// method case
struct X
{
   inline int iDecDefC() {return 1;} // inline anyway
   inline int iDecDef();
   inline int iDec();
   int iDef();
};


How is that the code inline by default??? unless it is explicitly declared as inline as above, will it be considered inline by default???
If you put the function inside the struct/class, it is assumed to be inline.
"inline" is a request to the compiler to write the function body at the point of invocation. But if the function contains any loop, or the method is virtual or the body of the function body is large then compiler ignores the "inline" request and creates the function body with static linkage. So, if such a function is referred in multiple translational units then each unit gets its own copy of the method.

The concept of virtual functions and inline functions conflicts each other. Decision to make a method inline is taken at compile time whereas the virtual methods are invoked at run time. So compiler just ignores the inline request if the method is virtual.
But if the function contains any loop, or the method is virtual or the body of the function body is large then compiler ignores the "inline" request and creates the function body with static linkage.


This is not true. It's still up to the compiler whether or not it will be inlined.

Obviously if a function is called polymorphically it can't be inlined, but that doesn't mean that virtual functions can never be inlined.

For example:

1
2
3
B b;
b.func();  // not called polymophically, can still be inlined
   // even if func is virtual 


EDIT:

And I also don't see what having a loop has to do with anything.
Last edited on
Disch wrote:
And I also don't see what having a loop has to do with anything.


Apparently some compilers will simply never inline stuff if it contains a loop. I still think that it would be up to the compiler though...
I think, there are no specific rules put forward by C++ standard when to skip inlining. Its upto the compiler designers whether to inline or skip inlining of a function. However compiler designers still follows some rules as I mentioned.

One more scenario where inline is skipped:
When you have an inline method, but you use its function pointer.

Disch,

What I meant is, inlining is skipped when you have a virtual method and somewhere in code you invoke it polymorphically. In this scenario, compiler won't do inlining of that method even if you invoke without polymorphism.
Topic archived. No new replies allowed.