禁用非完全专用模板类的构造
Disable construction of non fully specialized template class
本文关键字:专用 更新时间:2023-10-16
我有这种模板类的结构。
我的目的是禁止创建那些不提供完整专业化的类:
class AbstractContainer
{
public:
virtual ~AbstractContainer() = default;
// declare here interface methods
};
template <class T>
class Container final : public AbstractContainer
{
public:
Container() = delete;
};
template <>
class Container<int> : public AbstractContainer
{
public:
Container() = default;
explicit Container(const int& type) : AbstractContainer(), type_(type){}
private:
int type_;
};
一切都很好
Container<int> a; // it works
Container<int> a(5); // it works
Container<char> a; // does not compile
但我注意到它是为这些情况编译的
Container<int> a(Container<char>());
Container<int> a(Container<CustomClass>());
如何避免这种情况?我确实想要一个复制构造函数,但不是错误的类型,理想情况下,我希望有同样的编译错误问题(我可以在某处使用断言,但不知道如何设置它)。
这似乎是C++最烦人的解析问题:
Container<int> a(Container<char>());
这实际上只是一个函数声明:它声明一个返回Container<int>
对象的函数a
,并获取指向返回Container<char>
但什么都不带的函数的指针。
由于它只是一个函数声明,它根本不实例化Container<char>
,因此您不会收到任何错误。
您可以使用大括号语法代替括号,即:
Container<int> a{Container<char>()};
Container<int> b(Container<char>{});
Container<int> c{Container<char>{}};
上面的代码确实声明了三个对象:a
、b
和c
。
实例化Container<>
的主模板时出现自定义错误
您可以将以下模板deferred_false
定义为:
template<typename>
struct deferred_false {
static constexpr auto value = false;
};
或简称为:
#include <type_traits>
template<typename> struct deferred_false: std::false_type{};
然后,在类模板的定义中放置一个使用此deferred_false<T>::value
的static_assert
:
template<class T>
class Container final : public AbstractContainer
{
static_assert(deferred_false<T>::value, "Trying to instantiate Container<>");
};
这样,当要实例化Container
的主模板时,断言将在编译时失败,但在主模板未实例化时不会失败,因为static_assert
的条件取决于模板参数(即:T
)。
也就是说,如果没有针对Foo
的Container
专业化,并且您有:
Container<Foo> a;
您将收到以下编译时错误:
错误:静态断言失败:尝试实例化
Container<>
相关文章:
- .cpp和.h文件中的模板专用化声明
- 调用专用模板时出错"no matching function for call to [...]"
- 模板专用化(按容器):value_type
- 静态数据成员模板专用化的实例化点在哪里
- 特征 3 类的模板专用化
- Visual Studio 2017 不允许我创建 C++ 专用模板
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- 具有常量引用参数的函数模板专用化
- 使用其他模板的模板专用化
- 使用专用显卡进行 OpenGL 渲染时帧速率较低
- 嵌套模板显式专用化
- 如何检查模板专用化是否是基本模板的子类?
- C++:部分模板专用化用例
- 通过依赖类型使用非类型模板参数的单类型模板参数类模板的部分专用化
- 使用对象的基类部分模板专用化对对象进行哈希处理::哈希
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 为什么依赖模板类型在部分专用化中不可推导?
- 如何为静态常量模板化专用整数值分配存储
- 模板类的部分模板专用化,如 std::function
- 合并一组模板专用化