【问题标题】:class template specialization with constraint / concept具有约束/概念的类模板特化
【发布时间】:2017-05-18 10:01:38
【问题描述】:

我尝试对类特化使用约束:

struct A {};
struct B {};
struct C {};

template<typename T>
concept bool AorB() {
    return std::is_same<T, A>::value || std::is_same<T, B>::value;
}

template<typename T>
class X {};

template<AorB T>
class X {};

int main() {
    X<A> x1; // error: redeclaration 'template<class T> class X' with different constraints class X {};
    X<B> x2;
    X<C> x3;
}

我不知道我是否在这里犯了错误,或者这通常是不可能的?

这种方法有什么替代方案?我可以使用 CRTP 对一个通用的基本模板进行专业化,但这对我来说看起来很丑。

【问题讨论】:

    标签: c++ c++17 c++-concepts


    【解决方案1】:

    这不是特化,您实际上是重新声明了主模板,这确实是一个错误。
    专业化看起来像这样:

    template<typename T>
    class X { };
    
    template<AorB T>
    class X<T> { };
    //     ^^^
    

    【讨论】:

      猜你喜欢
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 2016-10-05
      • 2012-05-25
      • 2021-05-29
      相关资源
      最近更新 更多