Re: default contructing an object of unknown type T
On Jul 30, 12:24 pm, "Francesco S. Carta" <entul...@gmail.com> wrote:
Hicham Mouline <hic...@mouline.org>, on 30/07/2010 11:49:54, wrote:
"Francesco S. Carta"<entul...@gmail.com> wrote in message
news:4c52aae2$0$30915$5fc30a8@news.tiscali.it...
Francesco S. Carta<entul...@gmail.com>, on 30/07/2010 12:17:52, wrote:
About your objection: every type has a copy constructor and an
assignment operator unless (for user created types) you forbid
its usage to the client code - even then, you're just
forbidding it, but the type still has a copy constructor and
an assignment operator (at least declared as private /
protected, if not defined && used somewhere by the internals).
No. At least not by what I would understand by "has".
In C++03, every type has a *declaration* of a copy constructor and
a copy assignment operator. But only a declaration. (And
depending on the contents of the class, the declaration might
take a non-const reference. In which case, I'm not sure that it
can be used in an expression like:
T value = T();
..) It doesn't have a definition (which is what I would
understand by "has") if the copy constructor is user declared,
and the user doesn't provide one, or if the type has a either a
non-static data member or a base of a type with an inaccessible
or ambiguous copy constructor. And the above expression
requires a copy constructor, not just a declaration.
In practice, of course, *most* types which don't support copy
aren't meant to be used as local variables anyway. (One major
exception is RAII classes, like mutex::lock.)
If you want to allow copy-forbidden and assignment-forbidden
classes into that template, you could make them to allow
default initialization using zero and use this line instead:
const T t(0);
this will ensure the compiler will not interpret it as a
function declaration, always allowing built-in types in.
But it will impose an additional constraint on user defined
types. Probably more constraining than supporting copy.
--
James Kanze