部分模板特化错误

时间:2017-05-16 22:46:57

标签: c++ c++11 templates c++14 template-meta-programming

我正在编写一个容器类,对于较小的大小,阈值使用std :: array作为内核,较大的大小使用std :: vector。

这是我的实施。

    template<typename, size_t, bool>
    class VectorStorage {
    };

    template<typename T, size_t DIM>
class VectorStorage<T, DIM, bool_const_v<(DIM < threshold)> > : public kernel_t<T,
        DIM, 1> { // impelementaion}

template<typename T, size_t DIM>
class VectorStorage<T, DIM, bool_const_v<(DIM >= threshold)> > : public kernel_t<T,
        DIM, 1> { // impelementaion}

我收到以下错误?鉴于SFINAE不能为clas specialiaziton工作,它甚至可以做到这一点吗?我正在使用clang -std = c ++ 1y

  

非类型模板参数取决于模板参数   部分专业化

1 个答案:

答案 0 :(得分:3)

将计算放入默认模板参数中。

template<typename T, size_t DIM, bool ISSMALL = (DIM < threshold)>
class VectorStorage {
   /* default implementation for small case */
};

template<typename T, size_t DIM>
class VectorStorage<T, DIM, false> {
   /* implementation for big case */
};

您也可以切换它们,或使用两个部分特化。如果你是偏执狂并想要隐藏ISSMALL,请添加一个包装。