Re: Template conversion and default copy constructor
Matthias Hofmann wrote:
"James Kanze" <kanze.james@neuf.fr> schrieb im Newsbeitrag
news:e9u1kk$hu8$1@nntp.aioe.org...
Also, as another poster pointed out, this phrase has been taken
out of context. In context, it is clear that it means that the
compiler would never instantiate a
template< typename T> Test( T ) ;
with T equal Test. Which is not the case here either.
Now that I am reading 12.8/3 again, I understand your point,
and I think you are right. That passage should maybe reworded
to something like the following:
"A declaration of a constructor for a class X is ill-formed if
its first parameter is of type (optionally cv-qualified) X and
either there are no parameters or else all other parameters
have default arguments. Such an ill-formed constructor is
never instantiated from a member function template."
There's probably a better way to express it, but I guess you
know what I mean. It would prevent much confusion.
Agreed. I think the first paragraph in ?12.8 also needs some
work. What on earth does "conceptually [...] implemented by a
copy constructor" mean, for example. And when they say that "a
class object can be copied [...] by initialization", does it
mean any initialization, or just copy-initialization, or...
Section 8.5 goes into rather extreme detail as to how a compiler
should treat initialization; because of its detail, I rather
think it must have precedence over such vague statements as
those which have been quoted from ?12.8. But the statements in
?12.8 should certainly be formulated in a manner which doesn't
give rise to expectations of something different.
Also, although I think that ?8.5 is fairly clear in this case,
there are other cases where I'm less sure. Generally speaking,
are there cases where the compiler will always call the copy
constructor, without applying overload resolution. Consider,
for example, the third sub-bullet of the second bullet of
?8.5/14, particularly the sentence: "The result of the call
(which is the temporary for the constructor case) is then used
to direct-initialize, ACCORDING TO THE RULES ABOVE." The rules
above imply overload resolution. So given:
class Test
{
private:
Test( Test& ) ;
public:
Test( int ) {}
template< typename T >
Test( T const& ) { std::cout << "template" << std::endl ; }
} ;
int
main()
{
Test t1 = 5 ;
}
Is this code legal or not? If the compiler applies overload
resolution to the copy, it will choose the template constructor
(since the temporary cannot be bound to the non-const reference
of the copy constructor). g++ thinks it's legal, Sun CC no.
But if it's legal (and a strict interpretation of ?8.5/14 would
seem to say that it is), then I'm really lost as to the meaning
of the first paragraph of ?12.8. It's also rather
disconcerting; I'm used to the fact that a copy constructor can
be elided, and that any side effects in it may not take place,
but in this case, it is a non-copy constructor which is being
elided.
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]