Topic: Constructor template == constructor? (Was: templates as usual)
Author: wmm@world.std.com ("William M. Miller")
Date: Fri, 6 Jun 2003 21:06:22 +0000 (UTC) Raw View
"Scott Meyers" <Usenet@aristeia.com> wrote in message
news:MPG.1947fdd7768131a39896f4@news.hevanet.com...
> Where does the standard say that a member template for a constructor is a
> constructor? In general, a function template is not a function, so why
> should it be that a template for a constructor is a constructor?
There's no explicit statement to that effect, but it's clearly
assumed: 12.8p2 talks about a "non-template constructor," and the
accompanying footnote refers to "template constructors." The
clear implication is that both "template constructors" and
"non-template constructors" are "constructors."
It follows from the formal definition in 12.1p1, as well: "an
optional sequence of function-specifiers (7.1.2) followed by
the constructor's class name followed by a parameter list is
used to declare or define the constructor." A function template
declaration can satisfy those requirements as well as a
non-template function declaration.
-- William M. Miller
---
[ 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: stefan@nospam.slapeta.com ("Stefan Slapeta")
Date: Sat, 7 Jun 2003 17:47:39 +0000 (UTC) Raw View
"Scott Meyers" <Usenet@aristeia.com> wrote in
news:MPG.1949b23110674dc69896f5@news.hevanet.com...
>
> The more I think about this, I'm inclined to think that declaration of a
> template for a constructor *should* prevent the implicit declaration of a
> default constructor
I absolutely second this and I think that it was a great mistake not to
define a special behaviour for constructors - in my opinion they simple
can't be handled like normal function templates. It has never been clear for
me why a copy constructor template that should work for ANY type is NOT
called for the OWN CLASS because an implicit copy constructor is a better
choice here! This is absolutely not intuitive and has caused many troubles!
Stefan
---
[ 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: tslettebo@chello.no.nospam (=?Windows-1252?Q?Terje_Sletteb=F8?=)
Date: Sat, 7 Jun 2003 21:37:39 +0000 (UTC) Raw View
""Stefan Slapeta"" <stefan@nospam.slapeta.com> wrote in message
news:bbs3n5$42f$1@news.tuwien.ac.at...
>
> "Scott Meyers" <Usenet@aristeia.com> wrote in
> news:MPG.1949b23110674dc69896f5@news.hevanet.com...
> >
> > The more I think about this, I'm inclined to think that declaration
of a
> > template for a constructor *should* prevent the implicit declaration
of a
> > default constructor
>
> I absolutely second this and I think that it was a great mistake not
to
> define a special behaviour for constructors - in my opinion they
simple
> can't be handled like normal function templates. It has never been
clear for
> me why a copy constructor template that should work for ANY type is
NOT
> called for the OWN CLASS because an implicit copy constructor is a
better
> choice here! This is absolutely not intuitive and has caused many
troubles!
Well, it may make sense, as in the choice between an exact match
function, and a function template, the function is selected. Why
shouldn't the same hold for the copy constructor?
class test
{
public:
template<class T>
test(const T &); // Not used as copy constructor; implicit copy
constructor used
};
However, if it generates an implicit copy constructor, rather than
instantiating the template, how come it doesn't also generate an
implicit default constructor, when the template is there? This seems to
be inconsistent.
Regards,
Terje
---
[ 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: jpotter@falcon.lhup.edu (John Potter)
Date: Sat, 7 Jun 2003 22:54:14 +0000 (UTC) Raw View
On Sat, 7 Jun 2003 21:37:39 +0000 (UTC), tslettebo@chello.no.nospam
(Terje Sletteb=F8) wrote:
> However, if it generates an implicit copy constructor, rather than
> instantiating the template, how come it doesn't also generate an
> implicit default constructor, when the template is there? This seems to
> be inconsistent.
Consistent. Declaring a copy ctor prevents generation of an implicit
copy ctor. Declaring any ctor prevents generation of an implicit
default ctor. A template is never the copy ctor, but it is a ctor.
John
---
[ 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: tslettebo@chello.no.nospam (=?iso-8859-1?Q?Terje_Sletteb=F8?=)
Date: Sun, 8 Jun 2003 18:05:06 +0000 (UTC) Raw View
"John Potter" <jpotter@falcon.lhup.edu> wrote in message
news:uuo4evgbeccstvuf4okbaf0s7cpkop6gsf@4ax.com...
>On Sat, 7 Jun 2003 21:37:39 +0000 (UTC), tslettebo@chello.no.nospam
>(Terje Sletteb=F8) wrote:
>
>> However, if it generates an implicit copy constructor, rather than
>> instantiating the template, how come it doesn't also generate an
>> implicit default constructor, when the template is there? This seems
to
>> be inconsistent.
>Consistent. Declaring a copy ctor prevents generation of an implicit
>copy ctor. Declaring any ctor prevents generation of an implicit
>default ctor.
>A template is never the copy ctor, but it is a ctor.
Well, if a template is a constructor (as is the question in the subject
line), then it makes sense that it doesn't generate a default
constructor, and it's consistent, I agree. However, why, then, is a
template constructor not a copy constructor, as Stefan says? Neither is
a template assignment operator a copy assignment operator, it seems.
Does it have to do with what I also said, that it prefers (implicitly
generated) functions over function templates?
Regards,
Terje
---
[ 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: jpotter@falcon.lhup.edu (John Potter)
Date: Mon, 9 Jun 2003 02:05:06 +0000 (UTC) Raw View
On Sun, 8 Jun 2003 18:05:06 +0000 (UTC), tslettebo@chello.no.nospam
(Terje Sletteb=F8) wrote:
> "John Potter" <jpotter@falcon.lhup.edu> wrote in message
> news:uuo4evgbeccstvuf4okbaf0s7cpkop6gsf@4ax.com...
> >On Sat, 7 Jun 2003 21:37:39 +0000 (UTC), tslettebo@chello.no.nospam
> >(Terje Sletteb=F8) wrote:
> >> However, if it generates an implicit copy constructor, rather than
> >> instantiating the template, how come it doesn't also generate an
> >> implicit default constructor, when the template is there? This seems
> >> to be inconsistent.
> >Consistent. Declaring a copy ctor prevents generation of an implicit
> >copy ctor. Declaring any ctor prevents generation of an implicit
> >default ctor.
> >A template is never the copy ctor, but it is a ctor.
> Well, if a template is a constructor (as is the question in the subject
> line), then it makes sense that it doesn't generate a default
> constructor, and it's consistent, I agree. However, why, then, is a
> template constructor not a copy constructor, as Stefan says? Neither is
> a template assignment operator a copy assignment operator, it seems.
> Does it have to do with what I also said, that it prefers (implicitly
> generated) functions over function templates?
All I can see is that is has to do with what the standard says. Here is
an example which may help to confuse things. :)
struct S {
S () { }
S (S volatile&) { cout << "copy ctor\n"; }
template<class T>
S (T const&) { cout << "template ctor\n"; }
};
S f (S const& p) {
S s =3D p;
return e;
}
int main() {
S s1;
S s2 =3D s1;
S s3 =3D f(s1);
}
To construct s2 from s1, the copy ctor is best.
To construct s from p, the template ctor is only.
To construct the return value from s, the copy ctor is best.
To construct s3 from the return rvalue, the template ctor is only.
If the volatile copy ctor is replaced with the const copy ctor,
g++ removes two of the three copies. With the volatile, it only
removes one. It seems that it uses the second part of 12.8/15
to remove the return value because there is no requirement that
a copy ctor be used. It does not remove the copy from the
combined s return value to s3 because the first part of 12.8/15
only allows removing use of a copy ctor.
John
---
[ 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: stefan@nospam.slapeta.com ("Stefan Slapeta")
Date: Wed, 11 Jun 2003 19:19:18 +0000 (UTC) Raw View
"Terje Sletteb " <tslettebo@chello.no.nospam> wrote in message
news:JzBEa.10720$KF1.262725@amstwist00...
"John Potter" <jpotter@falcon.lhup.edu> wrote in message
news:uuo4evgbeccstvuf4okbaf0s7cpkop6gsf@4ax.com...
>>Consistent. Declaring a copy ctor prevents generation of an implicit
>>copy ctor. Declaring any ctor prevents generation of an implicit
>>default ctor.
It's consistent and so far it is still intuitive.
>>A template is never the copy ctor, but it is a ctor.
This isn't neither c. nor i. anymore and I think it was a mistake to define
it this way. I think I expressed myself a little bit unclear before - I
wanted to suggest that the "templated copy constructor" (that is a template
constructor that has the same syntax as a copy ctor + free right hand type)
should win over the default copy ctor. "Template" means "every type", and
it's not clear why there should be an implicit (=invisible) ctor just for
the OWN type (if you wish a special behaviour, you could still define a
regular copy ctor). Actually, that has been a pitfall for many template
newcomers (...and I remember something like 'Trivial solutions to trivial
beginners' problems' in Bjarne's talk about directions for C++ 0x :-)
I just think it would be very hard to change this nowadays...
> Well, if a template is a constructor (as is the question in the subject
> line), then it makes sense that it doesn't generate a default
> constructor, and it's consistent, I agree. However, why, then, is a
> template constructor not a copy constructor, as Stefan says? Neither is
> a template assignment operator a copy assignment operator, it seems.
> Does it have to do with what I also said, that it prefers (implicitly
> generated) functions over function templates?
It does and IMHO it was a mistake not to make an exception here.
Stefan
---
[ 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: Usenet@aristeia.com (Scott Meyers)
Date: Wed, 4 Jun 2003 22:55:22 +0000 (UTC) Raw View
On Fri, 18 Apr 2003 17:45:09 +0000 (UTC), Ron Natalie wrote:
> ""Razvan Cojocaru"" <razvanco@gmx.net> wrote in message news:b7p3hg$39565$1@ID-135723.news.dfncis.de...
>
> 12.1p6 says that any constructor, even templated ones inhibit the
> default.
>
> > 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).
Where does the standard say that a member template for a constructor is a
constructor? In general, a function template is not a function, so why
should it be that a template for a constructor is a constructor?
Scott
---
[ 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: kanze@gabi-soft.fr
Date: Thu, 5 Jun 2003 18:01:14 +0000 (UTC) Raw View
Usenet@aristeia.com (Scott Meyers) wrote in message
news:<MPG.1947fdd7768131a39896f4@news.hevanet.com>...
> On Fri, 18 Apr 2003 17:45:09 +0000 (UTC), Ron Natalie wrote:
> > ""Razvan Cojocaru"" <razvanco@gmx.net> wrote in message
> > news:b7p3hg$39565$1@ID-135723.news.dfncis.de...
> > 12.1p6 says that any constructor, even templated ones inhibit the
> > default.
> > > 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).
> Where does the standard say that a member template for a constructor
> is a constructor? In general, a function template is not a function,
> so why should it be that a template for a constructor is a
> constructor?
Good question. On the other hand, if you use the templated constructor,
the instantiation IS a constructor, and so should inhibit the default
constructor. And I don't know how the compiler can determine that you
haven't instantiated the template in any of the other translation units.
--
James Kanze GABI Software mailto:kanze@gabi-soft.fr
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, T l. : +33 (0)1 30 23 45 16
---
[ 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: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Thu, 5 Jun 2003 22:15:19 +0000 (UTC) Raw View
"Scott Meyers" <Usenet@aristeia.com> wrote...
> On Fri, 18 Apr 2003 17:45:09 +0000 (UTC), Ron Natalie wrote:
> > ""Razvan Cojocaru"" <razvanco@gmx.net> wrote in message
news:b7p3hg$39565$1@ID-135723.news.dfncis.de...
> >
> > 12.1p6 says that any constructor, even templated ones inhibit the
> > default.
> >
> > > 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).
>
> Where does the standard say that a member template for a constructor is a
> constructor? In general, a function template is not a function, so why
> should it be that a template for a constructor is a constructor?
The "template for a constructor" becomes a constructor as soon
as it's instantiated to be considered in the overload resolution.
Since such instantiation if created would be a constructor, that
constructor inhibits the default. That's how I read it.
Victor
--
Please remove capital A's from my address when replying by mail
---
[ 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: Usenet@aristeia.com (Scott Meyers)
Date: Fri, 6 Jun 2003 21:03:38 +0000 (UTC) Raw View
On Thu, 5 Jun 2003 22:15:19 +0000 (UTC), Victor Bazarov wrote:
> The "template for a constructor" becomes a constructor as soon
> as it's instantiated to be considered in the overload resolution.
> Since such instantiation if created would be a constructor, that
> constructor inhibits the default. That's how I read it.
I understand your reasoning, but if that's how you read it, I'm wondering
what part you are reading. 12p1 seems pretty clear to me:
The default constructor (12.1), copy constructor and copy assignment
operator (12.8), and destructor (12.4) are special member functions. The
implementation will implicitly declare these member functions for a class
type when the program does not EXPLICITLY declare them, except as noted
in 12.1. [Emphasis mine.]
Can a constructor (implicitly) instantiated from an (explicit) template be
considered to have been explicitly declared?
On the other hand, 12.1p5 seems to allow a little more latitude.
The more I think about this, I'm inclined to think that declaration of a
template for a constructor *should* prevent the implicit declaration of a
default constructor, so I'm not objecting to how compilers behave in this
area. (Like they'd care if I did...) However, it's not clear to me where
the Standard says that this is correct behavior.
Scott
---
[ 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 ]