This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: overloaded templated ostream-operator: gcc can't find the right one.
- To: jma at cpk dot auc dot dk
- Subject: Re: overloaded templated ostream-operator: gcc can't find the right one.
- From: "Martin v. Loewis" <martin at loewis dot home dot cs dot tu-berlin dot de>
- Date: Wed, 1 Mar 2000 22:35:56 +0100
- CC: gcc-bugs at gcc dot gnu dot org
- References: <38BD3AA2.E5CF1A2F@cpk.auc.dk> <38BD81B8.113EEE2A@cpk.auc.dk>
> Here comes the right bug-report.
Maybe I'm a little slow tonite, but I still can't find a bug in the
compiler. The compiler says
template_ostream_test.cc: In function `class ostream & operator <<<pair<int,char> >(ostream &, pair<int,char> &)':
template_ostream_test.cc:56: instantiated from here
template_ostream_test.cc:9: no type named `iterator' in `struct pair<int,char>'
I hope you agree that this message is correct in the sense that an
instantiation of the template mentioned above, i.e. of
template<class T>
ostream& operator<<(ostream& os, T& cont); [with T=pair<int,char>]
would indeed cause these error messages: pair<int,char> really does
not have a nested type 'iterator', right?
So the question now is why the compiler uses the template. Well,
because the C++ standard says it should do so. Selecting an operator
proceeds in the following way:
1. Using lookup mechanisms, determine all candidates. This gives the
template, and the variant overloaded for pair<int,char>
2. If there are any templates found during lookup, perform template
argument deduction for each of those. For the template, this gives
T=pair<int,char>
3. Select the viable candidates. Both functions are viable.
4. Compute conversion sequences for each argument. The template
instantiation requires a reference binding (i.e. identity
conversion), the normal operator requires a reference binding and
a qualification conversion (pair<int,char> -> pair<int,char>
const&).
5. Determine the best viable function. The template instantiation is
better than the normal function, since an identity conversion is
better than a qualification conversion.
If you question this line of reasoning, please discuss it in one of
the public C++ fora first, eg. comp.lang.c++.moderated, or
comp.std.c++.
Regards,
Martin