This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: overloaded templated ostream-operator: gcc can't find the right one.


> 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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]