Topic: templates as usual


Author: razvanco@gmx.net ("Razvan Cojocaru")
Date: Fri, 18 Apr 2003 16:59:06 +0000 (UTC)
Raw View
Hello.

This is the code:

template<class T>
class A {
public:
    template<class X> A(const A<X>&) {}
};

int main()
{
    A<int> a;
}

Should this code compile with a 100% standard compliant C++ compiler?

>From looking in the standard and "The C++ PL" I found out that:
1. the member template copy constructor doesn't prevent the generation of a
default copy constructor
and
2. a default constructor is not generated by the compiler unless there is no
other constructor declared in the class.
I am still a bit puzzled though because since I haven't used a copy
constructor, and this being not only a template class, but with a member
template, no user defined constructor should exist in the instantiation for
int. So why isn't there a default constructor?

On top of that, the example is inpired by an example in mr. Josuttis' book
"The C++ Standard Library - A Tutorial and Reference", 9th printing -
Chapter 2, section 2.2, page 13, where he doesn't define a default
constructor although his example seems to imply that one is being
automatically generated.

Please explain the right behaviour. If possible, please point me to the
relevant sections of the standard, and if at all possible, explain the
reasons for the decision.

Thank you,
Razvan




---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ron@sensor.com ("Ron Natalie")
Date: Fri, 18 Apr 2003 17:45:09 +0000 (UTC)
Raw View
""Razvan Cojocaru"" <razvanco@gmx.net> wrote in message news:b7p3hg$39565$1@ID-135723.news.dfncis.de...

> 1. the member template copy constructor doesn't prevent the generation of a
> default copy constructor

A templated constructor is never the copy constructor even if it has the right
arguments.   The templated constructor doesn't surpress the implicitly generated
one.  12.8p2 says that templates are never copy constructors.

> 2. a default constructor is not generated by the compiler unless there is no
> other constructor declared in the class.

12.1p6 says that any constructor, even templated ones inhibit the default.  It's only the declaration
that matters (not the definition).

> I am still a bit puzzled though because since I haven't used a copy
> constructor, and this being not only a template class, but with a member
> template, no user defined constructor should exist in the instantiation for
> int. So why isn't there a default constructor?

You have a user declared constructor (the template one).





---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]