3

I was reading about inline functions in C/C++ from:

http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc07cplr243.htm

For the following code:

inline.h:
   #include<stdio.h>

   extern inline void two(void){  // GNU C uses this definition only for inlining
      printf("From inline.h\n");
   }

main.c:
   #include "inline.h"

   int main(void){
      void (*pTwo)() = two;
      two();
      (*pTwo)();
   }

two.c:
   #include<stdio.h>

      void two(){
      printf("In two.c\n");
   }

The output is given as :

   From inline.h
   In two.c

It says that this output is obtained for "Using the gcc semantics for the inline keyword".

How is version of two() function to be called decided in case one of the versions is inlined?

As i can see from the output, the inlined version is called with two() is directly invoked i.e. without any function pointer. Whereas, when a function pointer is used, the non-inlined version is called. Is there a general rule for resolving such calls?

4
  • That is a violation of the ODR rule. Your program has two different definitions for the same function Commented Jun 17, 2013 at 13:33
  • Does this not break the "one definition rule". But yes, the inline function is only used when the function is called DIRECTLY, when it is called through the pointer, it uses the one in two.c. Commented Jun 17, 2013 at 13:34
  • @MatsPetersson: Can you support this with documentation from the compiler vendor? This is a compiler extension and it could be weirder than that. Whether a function is inlined or not is usually determined through a set of heuristics, which means that even a direct call could use either option depending on the surrounding code Commented Jun 17, 2013 at 13:37
  • @DavidRodríguez-dribeas: It would appear that it depends on the version of the compiler, but here's a link for gcc 4.3 which alters this behaviour: gcc.gnu.org/gcc-4.3/porting_to.html Commented Jun 17, 2013 at 13:41

2 Answers 2

0

When using a function pointer, the callable must have an address. The compiler must then gives the function an address, therefore it will resolves to the non-inlined one.

I suppose the general rule is that you can't call an inlined function using a function pointer, so if 2 versions of the function exist, the non-inlined will be called when using a function pointer. Note that inline is just a tips for the compiler, and it might chose to ignore it. Because of this, I don't think (correct me if i'm wrong) you can do anything to ensure that the inlined version of the function will be called.

Of course, having two function named the same is probably a bad idea.

EDIT: Enabling optimization flags may change a few things. The inlined version may be called even when using the function pointer.

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

Comments

0

Yes, there is indeed a rule. It's in fact precisely what you suggest. This compiler extension allows two definitions of a single function to co-exist, and the compiler chooses which definition to use depending on whether it inlines the call to the function.

In particular, this implies that the first definition is always inlined, and the second never is. Which one will be used from a given call site therefore depends solely on the details of caller. In your case, the first call is direct, and the second is through a pointer. The compiler decided to inline the first call and not the second call. That is one of the 4 possible decisions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.