Templated Copy Ctor

From: Rich (richbourne_at_hotmail.com)
Date: 12/03/03


Date: 3 Dec 2003 04:16:59 -0800

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



Relevant Pages

  • Re: Read a textfile to an array
    ... I need some help with writing a member function really badly. ... > template class with the constructor as follows: ... as a variable that's local to the ctor, so as soon as the ctor ... > was created by the constructor. ...
    (microsoft.public.vc.language)
  • Re: Templated copy constructor
    ... Hmm, I thought there is no such thing like a "template copy ctor", ... regardless of a probably matching signature. ...
    (comp.lang.cpp)
  • Problem with template ctors (smart pointer)
    ... // template smart pointer like ... my test function which doesn't compile: ... When i remove ctor 4 it compiles, ...
    (comp.lang.cpp)
  • Re: c to c++
    ... to form, they refused to fix), long replaced. ... MS still gets grief for the template support in VC6 (despite being ... a compiler option to do it either way). ...
    (comp.lang.c)
  • Re: Need help with templates and VC6
    ... Alex Blekhman wrote: ... The support of member templates in VC6 is rather basic. ... VC6 and eVC4 are not able to correctly parse the explicit template arguments ...
    (microsoft.public.vc.language)