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: suspicious overload resolution of function templates


> It's probably more a question than a bug report: can anybody be so
> patient to explain me why in the following constellation the
> template #1 is being chosen in both calls? Anyhow, so does the gcc
> 2.95.2.

Even though the treatment of references in partial ordering is subject
to an ongoing heated debate, your example is quite clear, I think.

> // #1
> template <class X> void f(X x);
> 
> // #2
> template <class X> void f(const A<X>& x);
> 
> // #3
> void f(const C& x);
> 
> int main()
> {
>    B<int> b;
>    D d;
>    f(b), f(d);
>    return 0;
> }
> 

> I thought, #2 is more specialized than #1, and #3 should be moreover
> preferred as being a non-template.

In the call to f(b), template argument deduction is performed for #1
and #2. Combining this with the lookup, we get
void f<B<int> >(B<int>);         #1a
void f<int> (const A<int>&);     #2a
void f(const C&);                #3

Of those, #3 is not a viable candidate, so it is eliminated from the
candidates. Then, overload resolution has to chose between #1a and
#2a.  For #1a, we get an identity conversion, for #2a, we get a
derived-to-base Conversion. So #1a has a better conversion sequence,
and we don't care about partial ordering - that would be only
considered if the conversion sequences would not be better.

In the call f(d), the situation is similar; the deduced signature is
void f<D> (D);

so again you have an identity conversion for this function, and a
derived-to-base conversion for f(const C&) (in addition to the
qualification conversion).

> If the compiler behaves correctly, how can I otherwise manage to
> prevent the "last resort" specialization (#1) from shadowing the
> others (#2 and #3) which should handle objects from entire
> hierarchies of classes, not just of the base class?

I don't know; I don't think there is a solution. Please discuss it in
one of the public C++ fora, eg. comp.lang.c++.moderated, or
comp.std.c++. If anybody knows an answer, please let me know.

Regards,
Martin

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