A template declaration/definition question

P

persres

Hi,
I incorrectly definted a member function as follows :
ABC.h :
void ABC<class T>::func1()
{

}


I guess I should have defined it as :
template <class T>
void ABC<T>::func1()
{

}

Can you please tell me what the first one means? I never knew that had
any meaning. It was a mistake by me as I have not been programming for
over 4 years. However, it compiles. When I include that in multiple
cpp files, while linking I get multiple definitions - As though the
compiler treats it as a plain old function and not as a template.

I would think the first one should not compile as its incorrect
syntax. I am using VC2008.
Any help please.
Thanks
 
V

Victor Bazarov

Hi,
I incorrectly definted a member function as follows :
ABC.h :
void ABC<class T>::func1()
{

}


I guess I should have defined it as :
template<class T>
void ABC<T>::func1()
{

}

Can you please tell me what the first one means? I never knew that had
any meaning. It was a mistake by me as I have not been programming for
over 4 years. However, it compiles. When I include that in multiple
cpp files, while linking I get multiple definitions - As though the
compiler treats it as a plain old function and not as a template.

I would think the first one should not compile as its incorrect
syntax. I am using VC2008.
Any help please.

I think if you omit 'template<class T>', you aren't defining a template,
you're defining the definition for a particular specialization. And
since you added 'class' there, the compiler thinks that you are
declaring that at some point there will be a class named 'T' for which
it is supposed to use that particular piece of code when you use that
member of the template. Since you never attempted to use the type
itself in the function (either in the argument list or the body), you
never gave the chance to the compiler to complain about an undefined type T.

V
 
B

Bo Persson

Hi,
I incorrectly definted a member function as follows :
ABC.h :
void ABC<class T>::func1()
{

}


I guess I should have defined it as :
template <class T>
void ABC<T>::func1()
{

}

Can you please tell me what the first one means? I never knew that
had any meaning. It was a mistake by me as I have not been
programming for over 4 years. However, it compiles. When I include
that in multiple cpp files, while linking I get multiple
definitions - As though the compiler treats it as a plain old
function and not as a template.

It doesn't mean anything at all, as a specialization would have a
template<> prefix. Interestingly, if you select "Disable Language
Extensions" it will tell you that!

So, presumably the compiler accepts this non-standard specialization
syntax just because it always has.


Bo Persson
 
J

Johannes Schaub (litb)

Victor said:
I think if you omit 'template<class T>', you aren't defining a template,
you're defining the definition for a particular specialization. And
since you added 'class' there, the compiler thinks that you are
declaring that at some point there will be a class named 'T' for which
it is supposed to use that particular piece of code when you use that
member of the template. Since you never attempted to use the type
itself in the function (either in the argument list or the body), you
never gave the chance to the compiler to complain about an undefined type
T.

Yes, i think you are right. It's an elaborated type specifier, and like you
said it specifies the class-type "T". Most often, people will not notice
this mistake when they do

template<typename T>
void ABC<class T>::f() { }

On some compilers, because they incorrectly accept elaborated type
specifiers that refer to template parameters (normally that snippet is ill-
formed). In those cases for those compilers it will be equivalent to "T". In
the case of this guy, there was no name "T" declared, so a new class name
was declared by that elaborated type specifier.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top