gpt4 book ai didi

c++ - 部分模板特化错误

转载 作者:行者123 更新时间:2023-11-27 23:54:00 25 4
gpt4 key购买 nike

我正在编写一个容器类,对于小于阈值的大小,使用 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,甚至可以这样做吗?我正在使用带有 -std=c++1y 的 clang

non-type template argument depends on a template parameter of the partial specialization

最佳答案

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

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,请添加一个包装器。

关于c++ - 部分模板特化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44012809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com