 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gareth Stockwell Guest
|
Posted: Thu Aug 28, 2003 8:10 pm Post subject: Template classes and inline |
|
|
My question seems pretty trivial, but I can't see any black-and-white
answers so far in this newsgroup...
It concerns function inlining and template classes. Normally, when I
write such classes, I keep the implementation of the member functions
in a separate class to avoid cluttering up the header, like this:
// --- file myclass.h
#ifndef __MYCLASS_H
#define __MYCLASS_H
template<class T> class MyClass {
public:
MyClass();
void foo();
};
#include "myclass.hxx"
#endif
// --- file myclass.hxx
template<class T> MyClass<T>::MyClass() { }
template<class T> void MyClass<T>::foo() { /* do something */ }
Now, since the implementations of these functions are (by definition)
visible to the compiler when it processes any code which utilises
MyClass, does using the 'inline' keyword, e.g.
template<class T> inline void MyClass<T>::foo() { /* do something */ }
have any effect?
Gareth
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Fri Aug 29, 2003 11:11 am Post subject: Re: Template classes and inline |
|
|
"Gareth Stockwell" <gareth (AT) ebi (DOT) ac.uk> wrote in message
| Quote: | // --- file myclass.h
template<class T> class MyClass {
// --- file myclass.hxx
template<class T> MyClass<T>::MyClass() { }
template<class T> void MyClass<T>::foo() { /* do something */ }
Now, since the implementations of these functions are (by definition)
visible to the compiler when it processes any code which utilises
MyClass, does using the 'inline' keyword, e.g.
template<class T> inline void MyClass<T>::foo() { /* do something */ }
have any effect?
|
Yes, they prevent multiple definition errors. Say to files a.cpp and b.cpp
include myclass.hxx, both a.obj and b.obj could get definitions of
MyClass<int>::foo(). But many compilers these days have facilities to
eliminate duplicate object definitions, which happens (usually) even if you
declare the function inline, so it's possible you won't get an error.
However I do it like this
| Quote: | // --- file myclass.hpp
template<class T> class MyClass {
// --- file myclass.exp
export template<class T> MyClass<T>::MyClass() { }
export template<class T> void MyClass<T>::foo() { /* do something */ }
|
In the .exp file the export keyword does nothing, at least on my compiler
Borland 6.
Files a.cpp and b.cpp include myclass.hpp only. There is another file
instantiate.cpp that includes the exp files and instantiates the
MyClass<int> and whatever else is required for a succesful link.
Eventually, I think that because in the .exp file I use the export keyword,
the compiler/linker system should automatically generate an instantiate.obj
with the instantiations I need. But we'll have to wait for that feature.
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Adnan Guest
|
Posted: Mon Sep 01, 2003 11:58 am Post subject: Re: Template classes and inline |
|
|
| Quote: |
Now, since the implementations of these functions are (by definition)
visible to the compiler when it processes any code which utilises
MyClass, does using the 'inline' keyword, e.g.
template<class T> inline void MyClass<T>::foo() { /* do something */ }
have any effect?
|
Remember, inline keyword is just a hint to the compiler to inline the
function. It depends on the compiler implementation what it will choose to
do. A compiler may choose to inline a function even if you dont specify the
inline keyword. It may also not inline the function if not possible, even
though you have explicitly put inline keyword in the function declaration.
It is a good practice not to use inline keyword at all.
Adnan.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Agent Mulder Guest
|
Posted: Mon Sep 01, 2003 7:15 pm Post subject: Re: Template classes and inline |
|
|
<Adnan>
Remember, inline keyword is just a hint to the compiler to inline the
function. It depends on the compiler implementation what it will choose to
do. A compiler may choose to inline a function even if you dont specify the
inline keyword. It may also not inline the function if not possible, even
though you have explicitly put inline keyword in the function declaration.
It is a good practice not to use inline keyword at all.
</Adnan>
It's a sad affair how inline is going to take the same route that
auto and register took. See
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=biaml7%24dqh%241%40n
ews4.tilbu1.nb.home.nl&rnum=28
for a recent proposal to re-strengthen the inline specifier. Inline
should be matched by its counterpart 'unline' to give it back its
original and obvious meaning.
-X
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Tue Sep 02, 2003 2:59 pm Post subject: Re: Template classes and inline |
|
|
"Agent Mulder" <mbmulder_remove_this_ (AT) home (DOT) nl> writes:
| Quote: | Adnan
Remember, inline keyword is just a hint to the compiler to inline the
function. It depends on the compiler implementation what it will choose to
do. A compiler may choose to inline a function even if you dont specify the
inline keyword. It may also not inline the function if not possible, even
though you have explicitly put inline keyword in the function declaration.
It is a good practice not to use inline keyword at all.
/Adnan
It's a sad affair how inline is going to take the same route that
auto and register took. See
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=biaml7%24dqh%241%40n
ews4.tilbu1.nb.home.nl&rnum=28
for a recent proposal to re-strengthen the inline specifier. Inline
should be matched by its counterpart 'unline' to give it back its
original and obvious meaning.
|
The post you refer to describes a technique ('java-shave') which clouds the
readability of a class declaration, obscuring the interface with
implementation detials, and unnecessarily increasing both compile
times and (in some cases) dependencies. That is IMO an abuse of
inline. If inline is being de-valued because of this abuse (an
assertion I don't find convincing, but your assertion seems to be
that the 'java-shave' is de-valuing inline), then people like you
need to stop abusing it. As for 'unline', why should a language
feature be added to support an abuse?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Manas Guest
|
Posted: Tue Sep 02, 2003 3:17 pm Post subject: Re: Template classes and inline |
|
|
[email]gareth (AT) ebi (DOT) ac.uk[/email] (Gareth Stockwell) wrote in message news:<d1b18d21.0308280047.574f1426 (AT) posting (DOT) google.com>...
| Quote: | My question seems pretty trivial, but I can't see any black-and-white
answers so far in this newsgroup...
It concerns function inlining and template classes. Normally, when I
write such classes, I keep the implementation of the member functions
in a separate class to avoid cluttering up the header, like this:
// --- file myclass.h
#ifndef __MYCLASS_H
#define __MYCLASS_H
template<class T> class MyClass {
public:
MyClass();
void foo();
};
#include "myclass.hxx"
#endif
// --- file myclass.hxx
template<class T> MyClass<T>::MyClass() { }
template<class T> void MyClass<T>::foo() { /* do something */ }
Now, since the implementations of these functions are (by definition)
visible to the compiler when it processes any code which utilises
MyClass, does using the 'inline' keyword, e.g.
template<class T> inline void MyClass<T>::foo() { /* do something */ }
have any effect?
|
when have a inline function definition in different module ie cpp
file, then inline keyword is required by the compiler to inline the
function. If not given it will not inline the function. Anyhow, inline
is just a compiler directive, it may choose not to inline the
function.
Also, if you have inline function definition in different module ie in
cpp file rather than in a header file and you use the header file in
someother cpp file , you will get "external unresolved linker error".
-manas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Early Ehlinger Guest
|
Posted: Wed Sep 03, 2003 12:47 am Post subject: Re: Template classes and inline |
|
|
"Gareth Stockwell" <gareth (AT) ebi (DOT) ac.uk> wrote:
| Quote: | Now, since the implementations of these functions are (by definition)
visible to the compiler when it processes any code which utilises
MyClass, does using the 'inline' keyword ... have any effect?
|
My understanding of inline is this:
1. The inline keyword has less effect than one might think on whether the
function shall be executed in-line (without a function call) or not. For
this purpose, inline is merely a hint to the compiler that it is allowed to
do in-lining.
2. The effect that the inline keyword has on the overall compile/link
process is that identical functions that are marked inline are not supposed
to conflict with each other. I.e., if I have two inline max(...) functions
that are defined identically, then I should not get "too-many-definition"
style linker problems.
3. If you take the address of a function that has been marked inline, then
you are guaranteed to have an out-of-line implementation somewhere
(otherwise you would have nowhere to address). This does not necessarily
mean that other calls of the function will be executed "out-of-line", just
that an out-of-line implementation exists.
4. On some compilers, template appears to implicitly mean "inline" as well,
while on others it does not. For example, on Borland C++ Builder 6.0, if I
have a template function that is called from several modules that is defined
without explicitly using inline, I do not get linker problems. On Visual
C++ 6.0, I must explicitly make the same template functions inline if they
are called from several modules, or I get linker problems. It's been so
long since I read the rules on templates that I do not know which compiler
is more Standards-Compliant on this point. I have a gut reaction that BCB
is, but that could be anti-VC6 bigotry...
--
-- Early Ehlinger CEO, ResPower Inc - Toll-Free : 866-737-7697
-- www.respower.com -- 500+ GHz Supercomputer Starting At USD$0.50/GHz*Hour
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Wed Sep 03, 2003 3:00 pm Post subject: Re: Template classes and inline |
|
|
In article <vl98s48kbgrk46 (AT) corp (DOT) supernews.com>, Early Ehlinger
<early (AT) respower (DOT) com> writes
| Quote: | 1. The inline keyword has less effect than one might think on whether the
function shall be executed in-line (without a function call) or not. For
this purpose, inline is merely a hint to the compiler that it is allowed to
do in-lining.
|
It is often described as 'a hint' but I think that the intent is better
described as 'a request'. Unlike 'an instruction' a request can be
refused but the refusal should be based on more than 'thanks for the
suggestion but I know what I am doing without any help from you.'
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Sep 03, 2003 3:24 pm Post subject: Re: Template classes and inline |
|
|
"Agent Mulder" <mbmulder_remove_this_ (AT) home (DOT) nl> wrote
| Quote: | Remember, inline keyword is just a hint to the compiler to inline
the function. It depends on the compiler implementation what it will
choose to do. A compiler may choose to inline a function even if you
dont specify the inline keyword. It may also not inline the function
if not possible, even though you have explicitly put inline keyword
in the function declaration. It is a good practice not to use
inline keyword at all.
It's a sad affair how inline is going to take the same route that auto
and register took.
|
Actually, it would be a very good thing if inline took the same route
that register took. There is (apparently -- not being concerned
directly, I've not verified) a problem in that it is taking the route in
the wrong order: compilers are starting to ignore inline before their
optimizers are good enough to justify it. But inline is an optimizing
measure, and ideally, the compiler would be able to generate the best
code without user intervention. I don't know when, if ever, we'll
attain that ideal, however.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Sep 03, 2003 3:25 pm Post subject: Re: Template classes and inline |
|
|
"Adnan" <adnan (AT) attbi (DOT) com> wrote
| Quote: | Now, since the implementations of these functions are (by
definition) visible to the compiler when it processes any code
which utilises MyClass, does using the 'inline' keyword, e.g.
template<class T> inline void MyClass<T>::foo() { /* do something */ }
have any effect?
Remember, inline keyword is just a hint to the compiler to inline the
function. It depends on the compiler implementation what it will
choose to do. A compiler may choose to inline a function even if you
dont specify the inline keyword. It may also not inline the function
if not possible, even though you have explicitly put inline keyword in
the function declaration.
|
That's what the standard says formally. IF the compiler can
systematically do better than the programmer in choosing what to inline,
it can and should ignore the hint. But that's a very big if -- there
are very, very few (if any) compilers today where this is the case. As
for the others (the vast majority), if they don't make at least a
reasonable effort to generate the code inline, they are not doing their
job.
| Quote: | It is a good practice not to use inline keyword at all.
|
In most cases. I never use inline, but then, I'm not writing low level
libraries, and all of my applications are IO bound anyway. (I suspect
that this is the case for the vast majority of programmers. But
certainly not all.) If the profiler says you need to improve the
performance in a tight loop which includes a function call, inline is
one of the cheapest and safest ways to do so. And there are certain
cases, such as library development, where it could be good practice to
use inline before profiling -- I would consider an implementation of the
standard library in which std::vector<>::operator[] wasn't inline
deficient (unless, of course, it were developed exclusively for that
rare compiler which legitimately ignores inline).
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|