473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Templates and Copy Constructors. Again.

Hi, all. I have the same old problem about templates and
copy constructors. I know this has been addressed hundreds
of times, but despite perusing many old postings, and The
Standard as well, I'm still feeling confused.

Suppose I have a class template:

template <typename T>
class Foo {

I think I declare the copy cconstructor as follows:

Foo(const Foo & another);

And if I do not declare it that way, the compiler writes a
copy constructor for me. Is that right?

Now, if I understand correctly, "Foo" is not a type, but a
template. So what does it mean that this function I'm
declaring takes a const reference to a Foo, when Foo is not
a type? Is it that "Foo", inside the class definition, is
a synonym for the type of the current object?

If so, does that mean I can do this (inside the class
definition):

int bar(Foo x);

Can I declare the copy constructor differently? In
particular, are any of the following, written inside the
class definition, equivalent to the above:

Foo(const Foo<T> & another);
Foo<T>(const Foo & another);
Foo<T>(const Foo<T> & another);

Now say I want to implement my copy constructor outside the
body of the class definition. How do I write the definition
of the function?

Thanks for any help. I know that a reasonable answer to
some of my questions above is "Try it out, you ninny!"
However, I want to write standard-conforming code, and I am
worried that my compiler and The Standard might not agree
on some points.

Jul 23 '05 #1
3 1893
"Matt Bitten" <mb*******@yahoo.com> wrote...
[...]
Suppose I have a class template:

template <typename T>
class Foo {

I think I declare the copy cconstructor as follows:

Foo(const Foo & another);

And if I do not declare it that way, the compiler writes a
copy constructor for me. Is that right?
Yes.
Now, if I understand correctly, "Foo" is not a type, but a
template. So what does it mean that this function I'm
declaring takes a const reference to a Foo, when Foo is not
a type? Is it that "Foo", inside the class definition, is
a synonym for the type of the current object?
'Foo' outside the template is a template-id. Inside the template
definition, 'Foo' by itself is "the same type as the one being
instantiated". IOW, if you write Foo<int> somewhere, and then
make it so its copy c-tor is used, then the "real" copy c-tor will
have this signature:

Foo<int>::Foo<int>(const Foo<int>& otherfoo);
If so, does that mean I can do this (inside the class
definition):

int bar(Foo x);
You absolutely can do this. This statement declares a function
'bar' that takes one argument of the same type as the instantiated
template and returns an int.
Can I declare the copy constructor differently? In
particular, are any of the following, written inside the
class definition, equivalent to the above:

Foo(const Foo<T> & another);
Foo<T>(const Foo & another);
Foo<T>(const Foo<T> & another);

IIRC, inside the definition of {template<typename T> class Foo}
the type-id "Foo<T>" means exactly the same thing as "Foo".
Now say I want to implement my copy constructor outside the
body of the class definition. How do I write the definition
of the function?
template<class T> Foo<T>::Foo(const Foo& other)
{
... // whatever
}

Thanks for any help. I know that a reasonable answer to
some of my questions above is "Try it out, you ninny!"
However, I want to write standard-conforming code, and I am
worried that my compiler and The Standard might not agree
on some points.


There can be two points of view on this. While you are supposed
to write standard code, you are still using a very particular
compiler and your code should also conform to that compiler's
idiosyncrasies.

V
Jul 23 '05 #2
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.

Jul 23 '05 #3
"Matt Bitten" <mb*******@yahoo.com> wrote...
Now, what is it that is NOT a copy constructor, that has everyone so
Everyone? I'd probably refrained from speaking for *everyone*...
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?
[...]
It is not clear to me that this is what the standard says, however.


class Foo {
public:
template<class T> Foo(T const& t); // a template constructor
};

template<class T> class Bar {
public:
template<class U> Bar(const Bar<U>&); // a template constructor
};

The point is that in neither case the template constructor implements
or works in lieu of the compiler-generated copy constructor, although
you might think that if in the first case "T" is Foo, then the c-tor
becomes
Foo(Foo const& t);

and in the second case if U is the same as T, then it becomes

Bar<T>(const Bar<T>&);

The Standard says that it's not going to happen. In both classes Foo
and Bar<T>, the copy constructor that has a very specific signature
is going to be provided and will take over for all copy-construction
needs.

Victor
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
2
by: Bore Biko | last post by:
Dear, I am an ordinary C programmer and I am most interesed about dynamical data structuring and programming, I don't like to use matricess and rows, I like to program with practical programs...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
3
by: tschwartz | last post by:
I'm trying to write a stylesheet which removes nodes which are empty as a result of other template processing. For example, given the following xml, I'd like to: - remove all "b" elements -...
3
by: abhishek.smu | last post by:
Given an XML like: <root> <node>8</node> <node>21</node> <node>-7</node> <node>13</node> <node>43</node> <node>2</node> </root>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.