类模板的编译错误,但其专业化除外
Compile error for class template except its specializations
我想禁止所有类型的类模板,除了class专门用于使用静态断言的类型。请检查以下代码:
template <typename Type>
class foo {
static_assert(false, "Wrong Type");
};
template <>
class foo<int> {
};
int main()
{
foo<int> fi; // should compile
foo<double> fd; // should not compile
return 0;
}
但它不起作用。经过一些研究,我发现了一个问题来回答我的问题:通过static_assert 强制模板类型
我已经根据问题更新了代码:
template <typename Type>
class foo {
static_assert(sizeof(Type) == -1, "Wrong Type!");
};
template <>
class foo<int> {
};
int main()
{
foo<int> fi; // should compile
foo<double> fd; // should not compile
return 0;
}
我的问题是为什么第二个版本可以,为什么第二版本不行?
这么简单:
template <typename Type>
class foo; // just don't provide an implementation...
template <>
class foo<int> { };
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- 如何使用默认参数等选择模板专业化
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 为什么在班级专业化上会出现错误?
- 类模板的编译错误,但其专业化除外
- C 概念和模板专业化;如何获得用户友好的编译器错误
- 错误:类模板部分专业化包含无法推导的模板参数
- 功能指针数组(包括成员功能)投掷模板专业化错误
- 实例化错误后成员函数模板的专业化,以及成员函数的顺序
- 实例化错误后如何避免专业化
- Singleton模板专业化编译的奇怪错误
- g++错误与部分模板专业化
- 为什么未使用的部分专业化没有错误
- 带有外部错误的模板专业化
- 为什么模板专业化中的显式实例化会给我带来错误