Template problem porting from 7.0 to 7.1

J

Jay

Hi,

Can anyone tell me why the following code doesn't work on 7.1? It's been ok
with previous versions. The error given by the linker is:

rtwnd.obj : error LNK2019: unresolved external symbol "class TestT<bool>
__cdecl operator+(class TestT<bool> const &,class TestT<bool> const &)"
(??H@YA?AV?$TestT@_N@@ABV0@0@Z) referenced in function "void __cdecl
f(void)" (?f@@YAXXZ)


template <class T> class TestT
{
public:

friend TestT <T> operator+ (const TestT <T>&, const TestT <T>&);
};

template <class T> TestT <T> operator+ (const TestT <T>&, const TestT <T>&)
{
TestT <T> result;

return result;
}


void f ()
{
TestT <bool> t1, t2, t3;

t3 = t1 + t2;
}


Thanks,

Jay.
 
J

Jay

It seems that replacing

friend TestT <T> operator+ (const TestT <T>&, const TestT <T>&);

with

friend TestT <T> operator+ <T> (const TestT <T>&, const TestT <T>&);

cures the problem. But why? And why no compiler error? I've seen many
examples of the original code. Is this a change in the standard? I'm
confused :)

Jay.
 
C

Carl Daniel [VC++ MVP]

Jay said:
It seems that replacing

friend TestT <T> operator+ (const TestT <T>&, const TestT <T>&);

This declares that a namespace-scoped function named operator+ that accepts
and returns TestT said:
with

friend TestT <T> operator+ <T> (const TestT <T>&, const TestT <T>&);

This declares that an instantiation of a namespace scoped function template
named operator + which is instantiated with a single template parameter <T>
and accepts and returns TestT<T> is a friend. In your example, the
definition of operator+ that you supplied is a function template, so this is
the proper form of friend declaration.
cures the problem. But why? And why no compiler error? I've seen many
examples of the original code. Is this a change in the standard? I'm
confused :)

VC7 was wrong, VC7.1 is right. Under VC7, a non-template function and a
template instantiation were "mangled" to the same name at the linker level,
as long as the actual function parameters and return types agreed. VC7.1
correctly distiguishes these cases by mapping them to distinct names.

-cd
 

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

Top