Re: Templates and Copy Constructors. Again.

From: Matt Bitten (mbitten73_at_yahoo.com)
Date: 02/23/05


Date: 22 Feb 2005 16:35:54 -0800

Thanks for the help.

Now, what is it that is NOT a copy constructor, that has everyone so
confused? I read in the standard in 12.8 that a "template
constructor" is not a copy constructor. However, a global search of
the standard turns up no other use of the term "template
constructor". So what is a template constructor?

FWIW, I tried a few things out. I'm using VC++ 7.0. The following
works:

#include <iostream>

template <typename T>
class Foo {
public:
  Foo();
  Foo(const Foo & another);
  Foo & operator=(const Foo & another);
  ~Foo();
};

template <typename T>
Foo<T>::Foo()
{ std::cout << "default constructor" << std::endl; }

template <typename T>
Foo<T>::Foo(const Foo & another)
{ std::cout << "copy constructor" << std::endl; }

template <typename T>
Foo<T> & Foo<T>::operator=(const Foo & another)
{ std::cout << "copy assignment" << std::endl; return *this; }

template <typename T>
Foo<T>::~Foo()
{ std::cout << "destructor" << std::endl; }

int main()
{
  Foo<int> f1; // Try default constructor
  Foo<int> f2(f1); // Try copy constructor
  f2 = f1; // Try copy assignment
  // Implicitly try destructor (twice)
}

And the printout shows that I am successfully overriding all four of
the automatically generated functions.

I can change ALL of the copies of "Foo" with no "<" after them into
"Foo<T>", except for the one after "class", and it still works.

I cannot change any of the "Foo<T>" above into just "Foo". So Foo as
a namespace or a return type, outside the class definition, must be
"Foo<T>". Foo as a parameter type for a member function or the name
of a constructor or destructor, is fine, as is any use of Foo inside
the class definition.

It is not clear to me that this is what the standard says, however.



Relevant Pages

  • Re: Member function pointer template argument
    ... What do you need the last template parameter for? ... template< typename R, typename C, typename P1) ... Right, func is a variable, but Foo expects a type as second template ...
    (comp.lang.cpp)
  • Templates and Copy Constructors. Again.
    ... Suppose I have a class template: ... class Foo { ... Is it that "Foo", inside the class definition, is ... Can I declare the copy constructor differently? ...
    (comp.lang.cpp)
  • Re: PLEASE HELP : Template inheritance problem gcc 3.2.3
    ... > now suppose I have a template as: ... > What is the syntax for the constructor of B in this case, i.e., what goes into ... template <typename T> ... struct B: A ...
    (comp.lang.cpp)
  • Re: Calling a constructor from template class
    ... I have a template class defined something like the following. ... template <typename T> ... class ArrayClass ... I want this class to call the constructor on a peice of memory ...
    (microsoft.public.vc.language)
  • Re: Nested templates and friends
    ... which the template then uses. ... as a template constructor is never used as a copy ... Roger Leigh ... GPG Public Key: 0x25BFB848. ...
    (comp.lang.cpp)