Ori*_*ent 1 c++ templates template-specialization class-template c++11
我有一个类模板:
template< typename ...bounded_types >
struct variant {};
Run Code Online (Sandbox Code Playgroud)
但是想禁止有界类型的空列表,即variant<>必须在编译时禁止.我可以做以下事情:
template<>
struct variant<>;
Run Code Online (Sandbox Code Playgroud)
但它不太清楚:如果我的变体库包含大量标题,那么它是不明显的,上面的特化是否不是类的前向声明,定义在下面的某处.在我看来,理想的想象解决方案将是:
template<>
struct variant<> = delete;
Run Code Online (Sandbox Code Playgroud)
这在很大程度上是明确的,但遗憾的是,C++语法禁止这样做.
什么是满足所述意图的最明确的方式?
template<typename... bounded_types>
struct variant {
static_assert(sizeof...(bounded_types) > 0, "empty variant is illegal");
};
Run Code Online (Sandbox Code Playgroud)
看看它是如何失败的:http://coliru.stacked-crooked.com/a/c08bee816d2bc36c
看看它是如何成功的:http://coliru.stacked-crooked.com/a/b34ece864f770d24
| 归档时间: |
|
| 查看次数: |
169 次 |
| 最近记录: |