【问题标题】:Is a diagnostic required for unused member templates with ill formed default template parameters?具有格式错误的默认模板参数的未使用成员模板是否需要诊断?
【发布时间】:2020-12-08 06:54:50
【问题描述】:

考虑以下类模板:

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

使用模板参数T 实例化S 是否格式错误,而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&amp; 诊断实例化,我怀疑这是一个 gcc 错误。是这样吗,还是这个code 不需要诊断?

【问题讨论】:

    标签: c++ templates language-lawyer


    【解决方案1】:

    这是CWG1635

    1635。模板默认参数与函数默认参数有多相似?

    默认函数参数仅在需要时实例化。默认模板参数也一样吗?例如,下面的格式是否正确?

     #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);
     }
    

    另外,查找的效果是一样的吗?例如,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-01
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多