inline specifier within in class definition

Hi, Could anyone explain why some static member functions defined within class definitions are specified with inline keyword? For instance, in boos/serialization/export.hpp template <class Archive, class Serializable> struct export_impl { ... inline static void enable_load(mpl::false_) {} inline static void enable_save(mpl::false_) {} }; AFAIU what the C++ Standard says in chapter "9.3 Member functions", it should be enough to define a member function in its class definition, "in which case it is an inline member function". On the other side, the standard also uses this technique, for instance in example presented in "18.2.1.5 numeric_limits specializations". I'm having problem with catching what's the rationale of using extra inline specifier. I was trying to find answer in the guidelines, but it seems it isn't explained. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net Charter Member of OSGeo, http://osgeo.org

Mateusz Loskot wrote:
Hi,
Could anyone explain why some static member functions defined within class definitions are specified with inline keyword? ...
Hi, Check out the following discussion: "Is there a special reason to use inline before template functions?" http://article.gmane.org/gmane.comp.lib.boost.ublas/4638 Cheers, Milosz

Milosz Marian HULBOJ wrote:
Mateusz Loskot wrote:
Hi,
Could anyone explain why some static member functions defined within class definitions are specified with inline keyword? ...
Hi, Check out the following discussion: "Is there a special reason to use inline before template functions?" http://article.gmane.org/gmane.comp.lib.boost.ublas/4638
Thanks Milosz. However, I've not found clear answer in that thread. Looks like it's a kind of mix of historical relict of old pre-standard habits with disbelief today compilers are able choose right Best regards, -- Mateusz Loskot, http://mateusz.loskot.net Charter Member of OSGeo, http://osgeo.org

Mateusz Loskot wrote:
However, I've not found clear answer in that thread.
Strange. I assume your question is "Why are some static member functions defined within class definitions specified with inline keyword?" and the answer is "The only reasons for this is to try to influence the inlining behavior of the compiler." (+copy&paste from existing code...) This is what I call a clear answer. The questions 1) Whether it is necessary to try to influence the inlining behavior of the compiler, and 2) Whether the inline keyword actually does influence the inlining behavior of the compiler may be related questions, but the fact the some of us have a hard time believing the answers to these questions doesn't make the answer to the initial question less clear.
Looks like it's a kind of mix of historical relict of old pre-standard habits
I can't remember any reference to pre-standard habits in the answers in that thread. You probably mean compilers with poor optimization capabilities, but "pre-standard" does say something completely different.
with disbelief today compilers are able choose right
In my impression, this question wasn't settled in that thread. However, the supporters of the "you sometimes have to help the compiler" opinion were actually more willing to present experimental evidence than the people who thought this was superstition. The problem of this is that you don't end up with a definite answer when to use the inline keyword (I guess you were hoping for "never use the inline keyword unless explicitly required"), and that the mere presence of some (superfluous?) inline keywords will make more superfluous inline keywords appear by "copy&paste". Regards, Thomas

Thomas Klimpel wrote:
Mateusz Loskot wrote:
However, I've not found clear answer in that thread.
Strange. I assume your question is "Why are some static member functions defined within class definitions specified with inline keyword?" and the answer is "The only reasons for this is to try to influence the inlining behavior of the compiler." (+copy&paste from existing code...)
This is what I call a clear answer.
I understand it but isn't enough to define such member function within class definition? It should be, according to what the C++ standard states.
The questions
1) Whether it is necessary to try to influence the inlining behavior of the compiler, and 2) Whether the inline keyword actually does influence the inlining behavior of the compiler
may be related questions, but the fact the some of us have a hard time believing the answers to these questions doesn't make the answer to the initial question less clear.
I'm just trying to confront specification of inline behavior in the standard with use of in-class definition of member functions together with inline keyword I see in Boost code.
Looks like it's a kind of mix of historical relict of old pre-standard habits
I can't remember any reference to pre-standard habits in the answers in that thread. You probably mean compilers with poor optimization capabilities, but "pre-standard" does say something completely different.
Perhaps. I meant pre-standard as old compilers like Visual C++ <= 6.0 or old line of GCC 2.x.
with disbelief today compilers are able choose right
In my impression, this question wasn't settled in that thread.
http://article.gmane.org/gmane.comp.lib.boost.ublas/4641
However, the supporters of the "you sometimes have to help the compiler" opinion were actually more willing to present experimental evidence than the people who thought this was superstition.
The problem of this is that you don't end up with a definite answer when to use the inline keyword (I guess you were hoping for "never use the inline keyword unless explicitly required"), and that the mere presence of some (superfluous?) inline keywords will make more superfluous inline keywords appear by "copy&paste".
Well, sort of, yes. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net Charter Member of OSGeo, http://osgeo.org

Mateusz Loskot wrote:
Thomas Klimpel wrote:
Mateusz Loskot wrote:
However, I've not found clear answer in that thread.
Strange. I assume your question is "Why are some static member functions defined within class definitions specified with inline keyword?" and the answer is "The only reasons for this is to try to influence the inlining behavior of the compiler." (+copy&paste from existing code...)
This is what I call a clear answer.
I understand it but isn't enough to define such member function within class definition? It should be, according to what the C++ standard states.
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. 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-time 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. Regards, Luke

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.

Olivier Grant wrote:
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.
Oliver, Thanks for the interesting insigths. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net Charter Member of OSGeo, http://osgeo.org

Simonson, Lucanus 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. Specifying the inline keyword forces the compiler to make the function signature a weak symbol and eliminates the link time error.
Defining member functions directly within the class definition already has that effect. Such functions are implicitly inline according to the C++ standard. The "inline" keyword is useless there as far as the C++ standard is concerned. Now, maybe some compilers consider specifying it explicitly an additional hint with regards to inlining, and that's the real question of that thread.

Mathias Gaunard wrote:
Simonson, Lucanus 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. Specifying the inline keyword forces the compiler to make the function signature a weak symbol and eliminates the link time error.
Defining member functions directly within the class definition already has that effect. Such functions are implicitly inline according to the C++ standard.
The "inline" keyword is useless there as far as the C++ standard is concerned. Now, maybe some compilers consider specifying it explicitly an additional hint with regards to inlining, and that's the real question of that thread.
Bingo! That's the question, indeed. However, I haven't found definitive answer. Anyway, I suppose it should easier for the authors to answer why they used extra inline keyword in some of Boost libraries. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net Charter Member of OSGeo, http://osgeo.org

Simonson, Lucanus J wrote:
Mateusz Loskot wrote:
Thomas Klimpel wrote:
Mateusz Loskot wrote:
However, I've not found clear answer in that thread. Strange. I assume your question is "Why are some static member functions defined within class definitions specified with inline keyword?" and the answer is "The only reasons for this is to try to influence the inlining behavior of the compiler." (+copy&paste from existing code...)
This is what I call a clear answer.
I understand it but isn't enough to define such member function within class definition? It should be, according to what the C++ standard states.
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.
What you are saying here stays in contradiction with section 2 of chapter "9.3 Member functions" as well as with section 3 of chapter "7.1.2 Function specifiers" in the C++ standard.
Specifying the inline keyword forces the compiler to make the function signature a weak symbol and eliminates the link time error. [...]
Agreed. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net Charter Member of OSGeo, http://osgeo.org
participants (6)
-
Mateusz Loskot
-
Mathias Gaunard
-
Milosz Marian HULBOJ
-
Olivier Grant
-
Simonson, Lucanus J
-
Thomas Klimpel