Boost logo

Boost :

Subject: Re: [boost] inline specifier within in class definition
From: Olivier Grant (olivier.grant_at_[hidden])
Date: 2009-05-05 03:48:37


Don't really agree with Luke:

> Luke wrote:
> If, by some strange coincidence, you happen to be implementing a
> non-template class and leave out the inline keyword when defining a static
> member function in the header file as part of the class definition, or any
> other kind of function, for that matter, you end up with a multiple function
> definition link-time error when the header is included in multiple execution
> units.

Actually no, any function defined within the class definition will be
considered as having an implicit inline qualifier, independently of whether
that class is a template or not. Any non-inline function, member or
not, defined within a header file, will generate multiple
definition linkage errors as soon as that header file is included in at
least two translation units.

> Specifying the inline keyword forces the compiler to make the function
> signature a weak symbol and eliminates the link time error. Since it
> becomes more painful to forget the inline keyword in the cases where it is
> needed than to type it in the cases where it is not, people might err on the
> side of throwing it in even if the class is a template and all its functions
> are weak symbols regardless. When implementing header file only code there
> is no harm (in my opinion) in declaring every function inline, and
> consistently following that policy completely eliminates the chances of
> multiple definitions link-ti

Declaring every function as inline is bad practice as it can result in
performance degradation (cache misses, thrashing, spilling) and increased
code size. the inline keyword is meant to let the compiler know that it can
eventually inject the code of the inlined function in the caller code, not
to avoid multiple definition linkage errors. There is no valid reason to use
the inline keyword to avoid linkage errors as that can be done by moving any
non-inline code to source files.

> me error. I don't follow that policy, personally, since I write so few
> non-template functions these days, but I wouldn't criticize others for doing
> so. If a programming practice makes programming less error prone and does
> no harm other than a little extra typing and slightly more verbose code I
> would consider it a reasonable thing to do.

Well it does harm, or at best, might :)

To give my little point of view regarding Mateusz Loskot's question, I
actually think the inline keyword is sometimes found qualifying the
definition of in-class member functions (static or not) as the combinated
result of using the inline keyword when required to specify a function as
inline and the fact that most, if not all, compilers do not emit any type of
message regarding the uselessness of the keyword for in-class definitions.
Add to that the fact that some very old compilers (such as VC6) probably
required the inline keyword even for function defined in-class (but this is
just a guess), and you could have the beginning of an explanation.

Regards,

Olivier.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk