Can't understand template (Short Code I promise!)

T

Tomás

First of all, the following will compile for me without any problems:

class BearerOfType
{
public:

class SpecialType {};
};


template<class T>
class Base
{
public:

typedef typename T::SpecialType SpecialType;

int NormalMemberFunction() const;
};

template<class T>
int Base<T>::NormalMemberFunction() const
{
return 5;
}

int main()
{
Base< BearerOfType > k;
}

The compiler compiles it silently with no errors and no warnings.

Now, if I change the return type of the member function from "int" to
"SpecialType", then I get a weird syntax error. First of all here's the code
(copied verbatim with line numbers):

01: class BearerOfType
02: {
03: public:
04:
05: class SpecialType {};
06: };
07:
08:
09: template<class T>
10: class Base
11: {
12: public:
13: typedef typename T::SpecialType SpecialType;
14:
15: SpecialType NormalMemberFunction() const;
16: };
17:
18: template<class T>
19: SpecialType Base<T>::NormalMemberFunction() const
20: {
21: return SpecialType();
22: }
23:
24: int main()
25: {
26: Base< BearerOfType > k;
27: }

And here's what my compiler gave me:

toopie.cpp:19: syntax error before `<' token
toopie.cpp:19: `T' was not declared in this scope
toopie.cpp:19: template argument 1 is invalid
toopie.cpp:20: warning: ISO C++ forbids declaration of
`NormalMemberFunction'
with no type
toopie.cpp:20: non-member function `int NormalMemberFunction()' cannot have
`
const' method qualifier
toopie.cpp: In function `int NormalMemberFunction()':
toopie.cpp:21: `SpecialType' undeclared (first use this function)
toopie.cpp:21: (Each undeclared identifier is reported only once for each
function it appears in.)


I was sitting there scratching my head thinking what could be wrong... until
I changed the function definition from:

template<class T>
SpecialType Base<T>::NormalMemberFunction() const

to:

template<class T>
Base<T>::SpecialType Base<T>::NormalMemberFunction() const


Now it compiles, but it gives a warning:

toopie.cpp:20: warning: `typename Base<T>::SpecialType' is implicitly a
typename
toopie.cpp:20: warning: implicit typename is deprecated, please see the
documentation for details


Anyone know what's going on here? Any advice appreciated.


-Tomás
 
M

Martin Vejnar

Tomás said:
[working code snipped]

01: class BearerOfType
02: {
03: public:
04:
05: class SpecialType {};
06: };
07:
08:
09: template<class T>
10: class Base
11: {
12: public:
13: typedef typename T::SpecialType SpecialType;
14:
15: SpecialType NormalMemberFunction() const;
16: };
17:
18: template<class T>
19: SpecialType Base<T>::NormalMemberFunction() const

typename Base said:
20: {
21: return SpecialType();
22: }
23:
24: int main()
25: {
26: Base< BearerOfType > k;
27: }

[error messages and incomplete resolution snipped]
 
T

Tomás

typename Base<T>::SpecialType Base<T>::NormalMemberFunction() const


Thanks.

Okay it works; but in my opinion, it's horribly verbose! I have to write
out the long-winded:

typename Base<T>::SpecialType

instead of:

SpecialType

-Tomás
 
J

Josh Sebastian

Tomás said:
Thanks.

Okay it works; but in my opinion, it's horribly verbose! I have to write
out the long-winded:

typename Base<T>::SpecialType

instead of:

SpecialType

Well, yeah... you're outside the class definition, so members of the
class must be qualified with the classname. If Base weren't a template,
you'd be able to save yourself the "typename" part, but that's it. If
you define the function inline, you can save yourself the trouble.

Josh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top