Templated Copy Ctor

R

Rich

Hi,

I have a query regarding VC6 and its handling of templated copy
constructors. Here goes:

Take a look at the following code sample...


template<class _Ty, size_t t_uiSize = 10 >
class my_template
{
public:

my_template()
{
printf("construct\r\n");
}

my_template( const my_template<_Ty,t_uiSize> &mt)
{
printf("construct by copying\r\n");
}

template<class _Other>
my_template(const my_template<_Other, t_uiSize> &mt )
{
printf("construct from a related class\r\n");
}
};




class myclass
{

};

class myotherclass
{

};




int main(int argc, char* argv[])
{
my_template<myclass> var1; // ctor
my_template<myclass> var2 = var1; // copy ctor
my_template<myotherclass> var3 = var1; // ctor from related

return 0;
}


The non-templated copy ctor must be defined BEFORE the templated copy
ctor so that it would build without error. If you define them the
wrong way around, you get:

fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1794)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more
information

....however, if you define them with the non-templated copy ctor BEFORE
the templated copy ctor you get:

error C2535: '__thiscall
my_template<_Other,`template-parameter258'>::my_template<_Other,`template-parameter258'>(const
class my_template<_Other,`template-parameter258'> &)' : member
function already defined or declared

The compiler can't decern any difference between the two function
signatures. Weird. This works fine on VC7.

Now, if we add a "dummy" variable in the templated ctor's parameter
list, then everything compiles and works fine. eg:

template<class _Other>
my_template(const my_template<_Other, t_uiSize> &mt, int dummy = 1 )
{
printf("construct from a related class\r\n");
}

I'm looking for a reason for this, but suspect that it's due to poor
template handling by the vc6 compiler...again. It's important to note
that if the second tamplate parameter is removed (the default UINT
param) then everything works fine. Does anyone know what's going on
here?

Thanks,
Rich
 
R

Rob Williscroft

Rich wrote in
[snip (template code)]

When the compiler screams:
fatal error C1001: INTERNAL COMPILER ERROR
[snip]

I'm looking for a reason for this,

Its reasonable to assume:
but suspect that it's due to poor
template handling by the vc6 compiler...again.
[snip]

Does anyone know what's going on
here?

Yes you do. Note VC6 is (in computer years) a very old compiler,
be thankfull you've found a workaround.

If possible upgrade to VC 7.1, its highly conformant, and you can put
all this nonsence behind you.

Note VC 7.0 was (and is maybe) little better than VC6 (some bugs removed,
some added) 7.1 is a major improvement.

Rob.
 

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
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top