【发布时间】: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