http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54310

             Bug #: 54310
           Summary: Order of operations during overload resolution
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: zeratul...@hotmail.com


GCC accepts the following code:


template <typename T>
struct meta
{
    typedef typename T::type type;
};

struct S{};

template <typename T>
typename meta<T>::type foo(T, S);

int foo(int, int);      

int main()
{
    foo(0, 0);
}


Clang rejects this code with the following error:

test.cpp:4:22: error: type 'int' cannot be used prior to '::' because it has no
members
    typedef typename T::type type;
                     ^
test.cpp:10:10: note: in instantiation of template class 'meta<int>' requested
here
typename meta<T>::type foo(T, S);
         ^
test.cpp:10:24: note: while substituting deduced template arguments into
function template 'foo' [with T = int]
typename meta<T>::type foo(T, S);
                       ^

I believe the code is invalid (and clang's error is correct), for the following
reasons:

1. Template argument deduction should be performed on the template candidate
*before* to discarding it due to a type mismatch for the second parameter
(expected S, got int). Section 13.3.1/7 of the standard says (emphasis mine):
"In each case where a candidate is a function template, candidate function
template specializations are generated using template argument deduction. Those
candidates are *then* handled as candidate functions in the usual way. A given
name can refer to one or more function templates and also to a set of
overloaded non-template functions. In such a case, the candidate functions
generated from each function template are combined with the set of non-template
candidate functions."

2. Template argument deduction on the template candidate should fail with a
hard error (not SFINAE), because the error that occurs (T::type not being valid
for T = int) is not in the immediate context of the function type. (Section
14.8.2/8, emphasis mine: "If a substitution results in an invalid type or
expression, type deduction fails. [...] Only invalid types and expressions *in
the immediate context* of the function type and its template parameter types
can result in a deduction failure. [...]").


GCC does reject the following example:


template <typename T>
struct meta
{
    typedef typename T::type type;
};

template <typename T>
typename meta<T>::type foo(T);

int foo(int);      

int main()
{
    foo(0);
}


With the following error:

test.cpp: In instantiation of 'struct meta<int>':
test.cpp:8:24:   required by substitution of 'template<class T> typename
meta::type foo(T) [with T = int]'
test.cpp:14:10:   required from here
test.cpp:4:30: error: 'int' is not a class, struct, or union type
     typedef typename T::type type;
                              ^

suggesting that GCC already obeys point (2) above, and therefore the problem is
likely to be with point (1).

Reply via email to