Topic: member template constructors


Author: Terence Kelling <kelling@arlut.utexas.edu>
Date: 1998/07/07
Raw View
I have a few questions concerning member template constructors for class
templates. Say I have a class template which takes two type parameters.
I need a default copy constructor and a member template copy constructor
for copying classes with compatible types.  The code would be:

template <class A, class B>
class foo {

   public:
      foo(const foo& rhs);
      template <class comp_A, class comp_B> foo(const foo<comp_A,
comp_B>& rhs);
};

// normal copy constructor
template <class A, class B>
foo<A, B>::foo(const foo<A, B>& rhs) {

}

// member template constructor for compatible type
template <class A, class B> template <class comp_A, class comp_B>
foo<A, B>::foo(const foo<comp_A, comp_B>& rhs) {

}


My questions are:

1.)  According to Stroustrup's "The C++ Programming Language" 3rd ed.,
the default constructor is never defined from the member template
definition.  However, the compiler I am using says that foo(const
foo<A,B>& ) is declared twice.  I'm pretty certain this is a compiler
bug, but I just want to verify this is what the standard
defines.

2.)  Does the same process hold for operator=(const foo& rhs)?  If I
want an assignment operator to work as described for the copy
constructor, would I have to define:

   foo& operator=(const foo& rhs);
   template <class comp_A, class comp_B>
      foo& operator=(const foo<comp_A, comp_B>& rhs);

That is to say, is the default operator= not generated by the member
template operator= as in the case of the default copy constructor?
Stroustrup only mentioned the case for the copy constructor.

3.)  If I want to specialize the member template copy constructor for
the case when comp_B is an int, can I do it and would the syntax be as
follows?

// specialization for when the second compatible type is an int
template <class A, class B> template <class comp_A>
foo<A, B>::foo(const foo<comp_A, int>& rhs) {

}

The compiler I'm using doesn't yet support partial specialization.

Thanks,
Terence Kelling
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]