【发布时间】:2016-01-21 12:55:05
【问题描述】:
template <class T>
class Base {
static_assert(!std::is_default_constructible<T>::value,
"T must not be default constructible");
};
struct X1 : Base<X1> {};
struct X2 : Base<X2> {
X2() = default;
};
struct X3 : Base<X3> {
X3() {};
};
struct X4 : Base<X4> {
X4() : Base{} {};
};
struct Y1 {};
int main() {
// all compile. They shouldn't
X1 x1; X2 x2; X3 x3; X4 x4;
// all compile. They shouldn't:
Base<X1> bx1; Base<X2> bx2; Base<X3> bx3; Base<X4> bx4;
Base<Y1> by1; // static assert fires. This is the expected behavior
}
类级别的static_assert 不会为任何X 类触发。但适用于Y(不派生Base)
如果将static_assert 移动到Base 的构造函数中,它可以正常工作
如果T 派生自Base,那么is_default_constructible<T> 在类级别总是为假的原因是什么?
【问题讨论】:
标签: c++ language-lawyer typetraits crtp