https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102063
Bug ID: 102063
Summary: Default arguments should not be a SFINAE context
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Keywords: accepts-invalid
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: redi at gcc dot gnu.org
Target Milestone: ---
G++ compiles this invalid code:
struct base { };
template<typename T>
void
can_haz_base(base* = (T*)nullptr); // XXX
template<typename T, typename = void>
struct haz_base
{
static constexpr bool value = false;
};
template<typename T>
struct haz_base<T, decltype(can_haz_base<T>())>
{
static constexpr bool value = true;
};
struct X { };
static_assert( ! haz_base<X>::value, "" );
The default argument at XXX is not in "the immediate context of the function
type, its template parameter types, and its explicit-specifier", and so should
not result in substitution failure. I think it should be an error, although
there is implementation divergence. GCC and MSVC accept it.
Clang says:
def.C:5:23: error: cannot initialize a parameter of type 'base *' with an
rvalue of type 'X *'
can_haz_base(base* = (T*)nullptr);
^ ~~~~~~~~~~~
def.C:14:29: note: in instantiation of default function argument expression for
'can_haz_base<X>' required here
struct haz_base<T, decltype(can_haz_base<T>())>
^
def.C:21:18: note: during template argument deduction for class template
partial specialization 'haz_base<T, decltype(can_haz_base<T>())>' [with T = X]
static_assert( ! haz_base<X>::value, "" );
^
def.C:21:18: note: in instantiation of template class 'haz_base<X>' requested
here
def.C:5:23: note: passing argument to parameter here
can_haz_base(base* = (T*)nullptr);
^
1 error generated.
EDG says:
"def.C", line 5: error: default argument of type "X *" is incompatible with
parameter of type "base *"
can_haz_base(base* = (T*)nullptr);
^
detected during instantiation of "void can_haz_base<T>(base *) [with
T=X]" at line 21
"def.C", line 21: error: static assertion failed with ""
static_assert( ! haz_base<X>::value, "" );
^
2 errors detected in the compilation of "def.C".