4
votes

Consider the following class template:

template<typename T>
struct S 
{    
    template<auto = T()> 
    void f();
};

Is it ill formed to instantiate S with template parameters T, for which auto = T() is ill formed?

int main()
{
    S<int> a;    // ok
    S<int&> b;   // error
    S<int()> c;  // gcc ok, clang error
}

This seems to be the case, but the issue is with c, where S is instantiated with a function type. gcc is ok with this, while clang says:

error: cannot create object of function type 'int ()'

which makes sense. Since gcc does diagnose the instantiation with int&, I suspect this is a gcc bug. Is that right, or is a diagnostic not required for this code?

1

1 Answers

1
votes

This is CWG1635:

1635. How similar are template default arguments to function default arguments?

Default function arguments are instantiated only when needed. Is the same true of default template arguments? For example, is the following well-formed?

 #include <type_traits>

 template<class T>
 struct X {
   template<class U = typename T::type>
   static void foo(int){}
   static void foo(...){}
 };

 int main(){
   X<std::enable_if<false>>::foo(0);
 }

Also, is the effect on lookup the same? E.g.,

 struct S {
   template<typename T = U> void f();
   struct U {};
 };