c++ - 是否需要对具有格式错误的默认模板参数的未使用成员模板进行诊断?

标签 c++ templates language-lawyer

考虑以下类模板:

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/

相关文章:

c++ - 有没有一种简单的方法可以将 C++ 对象添加到现有的 Objective-C Cocoa 应用程序

C++ 构造函数语法

c++ - 错误 C2440 : '<function-style-cast>' : cannot convert from 'LinkedList<int>::Node *' to 'LinkedListIterator<const T>'

c++ - unique_ptr 之外的数组模板语法

c++ - `*--p` 在 C++03 中实际上是否合法(格式正确)

c++ - 混合 cout 和 printf 以获得更快的输出

C++ 模板返回类型取决于模板参数?

c++ - 具有多个模板继承的模板实例化

c++ - 从理论上讲,仅在动态分配中使用内存范围的意外重用来同步线程是否(错误地)合法?

c++ - 类模板特化优先级/歧义