c++ - 明确禁止具体类模板特化

标签 c++ templates c++11 template-specialization class-template

我有一个类模板:

template< typename ...bounded_types >
struct variant {};

但要禁止有界类型的空列表,即 variant<>必须在编译时禁止。我可以执行以下操作:

template<>
struct variant<>;

但不太清楚:如果我的变体库包含大量的头文件,那么就不清楚,上面的特化是否不是一个前向声明类,在下面某处定义。在我看来,理想的假想解决方案是:

template<>
struct variant<> = delete;

这在更大程度上看起来是明确的,但不幸的是,反过来,C++ 语法禁止了它。

满足所描述意图的最明确方式是什么?

最佳答案

template<typename... bounded_types>
struct variant {
    static_assert(sizeof...(bounded_types) > 0, "empty variant is illegal");
};

看看它是如何失败的:http://coliru.stacked-crooked.com/a/c08bee816d2bc36c
看看它是如何成功的:http://coliru.stacked-crooked.com/a/b34ece864f770d24

关于c++ - 明确禁止具体类模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26430282/

相关文章:

c++ - 正在分配指针的测试类析构函数?

c++ - 在现代 C++ 中实现专门的数据结构

c++ - RC 文件更改会默默失败; "Cannot open the resource file for edit"

c++ - enable_if_t 中包含折叠表达式的编译器错误

php - Laravel 4 - 多次扩展在主模板中声明的部分

c++ - 特化模板成员函数

c++ - map<string, int> 从 const 函数调用时抛出 std::out_of_range

c++ - 在继承的构造函数中仅 move 类参数

c++ - vector 中的删除函数不会删除指针?

c++ - 如何使用tinycthread做C++并发编程