How to un-inline a function defined in class?

I was looking through The C++ Programming Language today and I noticed it said that functions defined inside the class definition (rather than outside it and qualified with Classname::) are automatically taken to be inline functions.

Is there any way to take this behaviour off for a particular function? Like an 'uninline' prefix would do if it existed?
AFAIK, there is no such thing as uninline. Why would you want to do it? Just like with non-member functions, it's only a hint to the compiler, so the function(s) may not be inlined (especially if it's too complex or is declared virtual).
or is declared virtual


FWIW, even virtual functions can still be inlined depending on how they're called.

I just didn't want to give OP the wrong idea because you almost made it sound like 'virtual' was the uninline keyword he was looking for.
I remember in the WINAPI having seen something like NOINLINE. Try including Windows.h and see if there is such a thing.
closed account (zb0S216C)
There are compiler-specific attributes that can be used on function declarations which explicitly tell the compiler not to in-line a function regardless of where and how it's declared. In-line expansion is disabled by default with Microsoft's compiler, but with GCC, there's an attribute to explicitly stop in-lining:

 
void Function( ) __attribute__( ( noinline ) );

Wazzak
Last edited on
@Disch, yeah, you are right. Invoking a v-function directly (not through a pointer/reference) actually makes the compiler treat the function as non-virtual.
Thanks for these answers. As to why I'd want to do it - I like to define member functions inside the class because I think it looks tidier. But I didn't like to think of the compiler generating awkward code to inline a function that isn't suitable to be inline.

But if the inline-ness of functions defined inside the class is just a hint, it doesn't matter.
Don't waste your time second guessing your compiler's optimizer and making your code less potentially portable than it needs be.
> I like to define member functions inside the class because I think it looks tidier

It really doesn't unless the function is very small; but I suppose that is a matter of opinion.

That defining every member function inside every class creates needless compile-time coupling is a not a matter of opinion, though.


> But if the inline-ness of functions defined inside the class is just a hint...

The inline specifier is not just a hint as far as ODR is concerned - an inline function with external linkage can be defined (identically) in multiple translation units.

The inline specifier as an optimization hint is completely ignored by mainstream compilers. With optimizations enabled, functions that are not declared inline may be inlined; and functions that are declared inline may not be inlined. The "as-if" rule is all that matters.
Topic archived. No new replies allowed.