Re: Template conversion and default copy constructor
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.
Good question. I would say that the code is legal, at least I cannot find
any text in 8.5/14 that says otherwise.
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.
The standard only says that "the copying can be elided". Apparently,
template constructors can be used for the direct-initialization part of
copy-initialization, or be elided.
I am confused about the meaning of 12.8/1 myself, but not more than I was
before I read your example.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]