考虑以下类模板:
template<typename T>
struct S
{
template<auto = T()>
void f();
};
实例化 S
格式不正确吗?带模板参数 T
,其中 auto = T()
畸形吗?int main()
{
S<int> a; // ok
S<int&> b; // error
S<int()> c; // gcc ok, clang error
}
似乎是这种情况,但问题出在 c
,其中 S
用函数类型实例化。 gcc 对此没问题,而 clang 说:error: cannot create object of function type 'int ()'
这是有道理的。由于 gcc 确实使用 int&
诊断实例化,我怀疑这是一个gcc错误。是对的,还是不需要此诊断 code ?
最佳答案
这是 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 {}; };
关于c++ - 是否需要对具有格式错误的默认模板参数的未使用成员模板进行诊断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63479624/