1

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?

1
  • 1
    If you're not sure what compiler generates, you can always use -S (and maybe -fverbose-asm for better readability) and check generated assembly. Result may be different for different compiler options and optimisation levels. Commented Jan 28, 2015 at 11:38

1 Answer 1

2

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

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

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.