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: partial ordering of function templates


Nathan Sidwell wrote:
> 
> Hi,
> this question concerns 14.5.5.2, where I think gcc is erroneously rejecting
> a valid program -- but I'm not sure.
> 
>         template <typename T> void Foo (T *); //a
>         template <typename T> void Foo (T &); //b
> 
>         void Baz (int *ptr)
>         {
>           Foo (ptr);
>         }
> 
> current/deduce.ii:6: call of overloaded `Foo(int*&)' is ambiguous
> current/deduce.ii:1: candidates are: void Foo(T*) [with T = int]
> current/deduce.ii:2:                 void Foo(T&) [with T = int*]
> 
> but I think the first choice is more specialized than the second. 14.5.5.2
> tells us to take each choice in turn, synthesize a unique type for each
> template type parameter into the function's parameter list and then try
> type deduction using the other choice. The transformed template is at least
> as specialized as the other if deduction succeeds and the deduced parameter
> types are an exact match. In this case that gives us, with a unique type
> called 'X'
> 
> a: Foo (T *) -> Foo (X *), using Foo (T &) deduce T to be X *, deduction succeeds.
> b: Foo (T &) -> Foo (X &), using Foo (T *) deduction fails
> 
> so b is less specialized than a, and void Foo(T*) [with T = int] should
> be selected
> 
> Am I forgetting something?

I agree with your analysis (I reported a similar testcase over a year ago), but
I believe Martin v. Loewis has some reservations. In the end Martin wound up
submitting a CWG issue (see
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#214).

Back then I discussed it with John Spicer of EDG who also thought it was
well-formed. 
Here's what current compilers do:

+-------------+-------------+
|gcc          |  AMBIGUOUS  |
+-------------+-------------+
|EDG eccp 2.45|          0  |
+-------------+-------------+
|HP aCC 3.26  |  AMBIGUOUS  |
+-------------+-------------+
|IBM xlC 5.0  |          0  |
+-------------+-------------+
|MSVC 6.0     |  AMBIGUOUS  |
+-------------+-------------+
|Metrowerks 5 |           0 |
+-------------+-------------+
|SunPro 5.2   |           1 |
+-------------+-------------+


template <typename T> int Foo (T *) { return 0; }
template <typename T> int Foo (T &) { return 1; }

int main ()
{
    int *ptr = 0;
    return Foo (ptr);
}


Regards
Martin

> 
> nathan
> --
> Dr Nathan Sidwell   ::   http://www.codesourcery.com   ::   CodeSourcery LLC
>          'But that's a lie.' - 'Yes it is. What's your point?'
> nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org

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