How can I tell gcc not to inline a function?

You want the gcc-specific noinline attribute. This function attribute prevents a function from being considered for inlining. If the function does not have side-effects, there are optimizations other than inlining that causes function calls to be optimized away, although the function call is live. To keep such calls from being optimized away, put asm (“”); … Read more

What’s the scope of inline friend functions?

When you declare a friend function with an unqualified id in a class it names a function in the nearest enclosing namespace scope. If that function hasn’t previously been declared then the friend declaration doesn’t make that function visible in that scope for normal lookup. It does make the declared function visible to argument-dependent lookup. … Read more

Can the linker inline functions?

In addition to the support for Link Time Code Generation (LTCG) that Jame McNellis mentioned, the GCC toolchain also supports link time optimization. Starting with version 4.5, GCC supports the -flto switch which enables Link Time Optimization (LTO), a form of whole program optimization that lets it inline functions from separate object files (and whatever … Read more

Does it make any sense to use inline keyword with templates?

It is not irrelevant. And no, not every function template is inline by default. The standard is even explicit about it in Explicit specialization ([temp.expl.spec]) Have the following: a.cc #include “tpl.h” b.cc #include “tpl.h” tpl.h (taken from Explicit Specialization): #ifndef TPL_H #define TPL_H template<class T> void f(T) {} template<class T> inline T g(T) {} template<> … Read more

Inline functions in C#?

Finally in .NET 4.5, the CLR allows one to hint/suggest1 method inlining using MethodImplOptions.AggressiveInlining value. It is also available in the Mono’s trunk (committed today). // The full attribute usage is in mscorlib.dll, // so should not need to include extra references using System.Runtime.CompilerServices; … [MethodImpl(MethodImplOptions.AggressiveInlining)] void MyMethod(…) 1. Previously “force” was used here. I’ll … Read more

When to use inline function and when not to use it?

Avoiding the cost of a function call is only half the story. do: use inline instead of #define very small functions are good candidates for inline: faster code and smaller executables (more chances to stay in the code cache) the function is small and called very often don’t: large functions: leads to larger executables, which … Read more

Inline functions vs Preprocessor macros

Preprocessor macros are just substitution patterns applied to your code. They can be used almost anywhere in your code because they are replaced with their expansions before any compilation starts. Inline functions are actual functions whose body is directly injected into their call site. They can only be used where a function call is appropriate. … Read more