Re: Frustrated in autocast failure
On Mar 9, 3:11 am, "Old Wolf" <oldw...@inspire.net.nz> wrote:
On Mar 5, 7:55 pm, adrian.hawry...@gmail.com wrote:
I have had a problem with templates and have removed the template and
made concreate functions and classes to do testing as to why I am
having a problem, but the problem still exists.
Here is my code:
Here is some simpler code that demonstrates the problem:
#include <iostream>
#include <string>
struct S
{
std::string z;
operator std::string () const { return z; }
// operator int *() const { return 0; }
};
int main()
{
S s;
std::cout << s;
}
The program fails to compile, unless the (int *) line is commented
back in. Perhaps someone more knowledgable than me can now comment
on what is going on.
But they already have. There is no non-template << which can be
called, and there is no template << for which template argument
deduction will succeed. Overload resolution only kicks in after
template argument deduction, on the non-template functions and
the instantiations of the template functions where template
argument deduction has succeeded.
(Note that the member operator<< are NOT function templates, and
template argument deduction does NOT apply to them. The type of
std::cout is known, and its non-member functions are taken into
consideration. Since operator<<( void* ) is a member, it is not
a template, and part of the overload set. operator<<(
std::string const& ) is not a member, however, and can only be
found if template argument deduction succeeds for it.)
It seems to me that S->(int *)->(void *) to match ostream::operator<<
(void *) is a trickier sequence than S->std::string to match
operator<<(ostream &, std::string const &) !
Actually, without the templates, the call would be ambiguous,
with the operator int*, since conversion to string and
conversion to int* both involve a user defined conversion. (Any
further disambuiguation only occurs if the same user defined
conversion is used, which isn't the case here.)
In fact the gcc output shows that the latter function is not
even in the candidates list.
It isn't, since template argument deduction fails.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
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! ]