https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104487

            Bug ID: 104487
           Summary: The substitution in the dependent name in a trailing
                    return type should cause recursive instantiation
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xmh970252187 at gmail dot com
  Target Milestone: ---

#include <iostream>
struct C{};
template<class T>
struct anotherType{
    using type = C;
};

template<class T>
auto fun(T)->decltype(fun(typename anotherType<T>::type{})){  //#1
    return 0;
}
int fun(C){  // #2
    return 0;
}
int main(){
   fun(0);  // #3
}

GCC incorrectly accepts this case and uses `fun<int>(int)` for the function
call at `#3`. The dependent name `fun` in the trailing return type will be
looked up in the instantiation context, since the type of the argument is class
type `C`, ADL would find `#1` and `#2` for that dependent name. Overload
resolution will perform again, this causes the substitution for `#1` again,
which will repeatedly perform the aforementioned step. The instantiation should
have exceeded the limits of the implementation. 


The weird thing is that the following variant example can cause that error:  

#include <iostream>
struct C{};
template<class T>
struct anotherType{
    using type = C;
};

template<class T>
auto fun(T)->decltype(fun(typename anotherType<T>::type{})){
    return 0;
}
int fun(int){  // in the above example, parameter type is `C`
    return 0;
}
int main(){
   fun(C{});   // in the above example, the argument is of type int
}

I do not find any explicit difference for looking up the dependent name since
the argument in the dependent name is always type `C` regardless of the
argument in the call at `#3`.

Reply via email to