Search code examples
cfunctiongcccompiledinline-functions

How to compile inline C function with literal attributes with GCC?


I have an inline function like this:

inline void func_1 (int a)
{
   if(a==1)
   {
      other_func1();
   }
   else
   {
      other_func2();
   }
}

and I use in the Main like this:

 int main()
 {
     func1(1);
     func1(42);

     return 0;
 }

I use GCC, I think, the compiled code look like this (in “source level”):

 int main()
 {
     other_func1()
     other_func2();

     return 0;
 }

Is it true or am I wrong?


Solution

  • Yes, in general gcc will optimise away dead code in inline functions when it can evaluate branches at compile-time. I use this construct a lot to allow optimised code to be generated for different use cases - somewhat like template instantiation in C++.