ciccio wrote:
Quote:
Okay I got this part now,
>
But ... when I use templates this is not the case.
What is not the case?
Quote:
Here is again an example (now without the static, since that does not
matter).
>
The compilation does not give any linking error here.
So what is the reason of this then? Does this mean that inline
functions of templates are just bogus?
Not "inline functions of templates", but "function templates declared
inline".
Right after defining your function and declaring it 'inline' (which
you're not really supposed to do, so your compiler _probably_ ignored
it), you _explicitly_ instantiate your template, which basically
introduces the definitions of all member functions (if it can, and it
should be able to, since you just defined your function), which are
of course _not_ inline.
IOW, whatever you declared 'inline' is *not* the function the linker
is looking for when resolving the reference from 'main'. It's looking
for 'foo<int>::bar', not for 'foo<T>::bar'. And your 'foo<int>::bar'
is non-inline (as the result of explicit instantiation).
Quote:
>
Regards
>
>
== foo.hpp ==
#ifndef _FOO_
#define _FOO_
>
template<typename T>
class foo {
public:
int a;
int bar(void) ;
};
#endif
== foo.cpp ==
#include "foo.hpp"
template<typename T>
inline int foo<T>::bar(void) { return a; }
template class foo<int>;
== main.cpp ==
#include "foo.hpp"
int main(void) {
int b = foo<int>().bar();
return 0;
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask