views:

63

answers:

3

Will this code:

inline int funcA(int a) __attribute__((always_inline))
{
    return a + 1;
}
inline int funcB(int b) __attribute__((always_inline))
{
    return funcA(b + 2);
}
int main()
{
    return funcB(3);
}

transformed to code like this?:

int main()
{
   return ((3) + 2) + 1;
}

GCC, ARM (iPhone)

A: 

There are no guarantees when using inline. It serves merely as a hint to the compiler (which in many (not all) cases have more heuristics and better understanding of the impact of inlining than the programmer).

aioobe
+1  A: 

Inlining function calls is not something the language requires compilers to do. It's a quality of implementation issue (QoI). But any of GCC, MSVC and clang will do it. Of course, you have to enable optimization.

For instance

# clang++ -cc1 -emit-llvm -O2 -o - main.cpp
define i32 @main() nounwind readnone {
entry:
  ret i32 6
}
Johannes Schaub - litb
A: 

Not necessarily. It depend's on the compiler and settings I guess. In fact in C++ for example it's not even guaranteed that this

inline int funcA(int a)
{
    return a + 1;
}
int main()
{
    return funcA(3);
}

will get transformed into this

int main()
{
    return 3 + 1;
}

inline is just the hint for the compiler. The compiler can ignore it.

Maciej Hehl
But you can force the compiler (GCC) to do it: inline void foo (void) __attribute__((always_inline));
tur1ng