Topic: Constructor template constructor? (Was: templates as usual)
Author: dhruvbird@gmx.net (Dhruv)
Date: Mon, 9 Jun 2003 16:22:43 +0000 (UTC) Raw View
tslettebo@chello.no.nospam (Terje Sletteb ) wrote in message news:<JzBEa.10720$KF1.262725@amstwist00>...
> 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?
>
Well, AFAICS, this is what is happening:
1. It sees that there is a template constructor and if it can be
instantiated, it will, and will prohibit the default form being
generated.
2. It checks to see if the user has a copy ctor, and/or an assignment
operator. If it does, nothing is done. If it doesn't, then one is
generated implicitly.
3. Now if there is a template copy ctor and/or a template assignment
operator defined, and the user wants to do something like this:
Eg A:
//foo is the name of a class.
foo<int> f1, f2;
foo<int> f3 = f1;
f2 = f3;
Here, it seems that it *prefers* the non-templates (overloaded) to the
temlates.
Eg B:
foo<int> f1, f2;
foo<double> f3 = f1;
f2 = f3;
Here, obviously, the templates are used.
This is similar to the overload resolution in this case:
template <class t>
void foo (t a)
{
cout<<"In template"<<endl;
}
void foo (int a)
{
cout<<"In normal version"<<endl;
}
int main ( )
{
foo (3.6); //template.
foo (3); //normal function.
return (0);
}
This is exactly what Terje said.
-Dhruv.
---
[ 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 ]