Depending on your compiler, yes. For g ++, the following will work:
void foo() __attribute__ ((noinline)); void foo() __attribute__ ((always_inline));
In MSVC ++:
__declspec(noinline) void foo(); __forceinline void foo();
Note that g ++ requires that attributes apply only to prototypes, not definitions. Therefore, if your function is for definition only (without a separate prototype), you must create a prototype to apply this attribute. MSVC does not have this requirement.
__forceinline
has some exceptions . Be sure to read them carefully so that you know if this will have any effect in your specific situation. g ++ does not document any exceptions to the always_inline
attribute, but some things are obvious (inserting a call to a virtual method will only work when the method is called statically, for example).
You can generalize this with macros:
#ifdef _MSC_VER #define NOINLINE(x) __declspec(noinline) x #define INLINE(X) __forceinline x #else #ifdef __GNUC__ #define NOINLINE(x) x __attribute__ ((noinline)) #define INLINE(x) x __attribute__ ((always_inline)) #else #error "I don't know how to force inline/noinline on your compiler." #endif #endif INLINE(void foo()); NOINLINE(void foo());
source share