c++ - 整数参数特殊值的模板特化

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

我正在为整数参数的模板特化而苦苦挣扎,也许这根本不可能?

我尝试了什么:

template< int COUNT, typename REMOVE, typename ...T>
struct RemoveFirstElementsImpl
{
    using Type= typename RemoveFirstElementsImpl<COUNT-1, T...>::Type;
};

template< typename ...T>
struct RemoveFirstElementsImpl<0, T...>
{
    using Type= TypeContainer<T...>;
};


template < int COUNT, typename ... T >
struct RemoveFirstElements
{
    using Type = typename RemoveFirstElementsImpl< COUNT, T...>::Type;
};

结果

error: partial specialization is not more specialized than the primary template because it replaces multiple parameters with a pack expansion

然后我想到了 SFINAE,比如:

template < int COUNT, typename = typename std::enable_if<COUNT==0>::type, typename HEAD, typename ... T >
struct RemoveFirstElements
{
    using Type= TypeContainer<T...>;
};
template < int COUNT, typename = void, typename HEAD, typename ... T >
struct RemoveFirstElements
{
    using Type= RemoveFirstElements<COUNT-1, T...>
};

但我不知道如何让参数包和默认参数的组合运行。

也许我走错了路。我想要实现的是获取一个参数列表,其中前 n 个参数从我的 TypeContainer 中删除,它只是一个扩展的 std::tuple。我只需要类型本身而不需要任何参数,我只需要类型而不需要任何对象。

最佳答案

我想你想要:

template<int COUNT, typename ...Ts> struct RemoveFirstElementsImpl;

template<>
struct RemoveFirstElementsImpl<0>
{
    using type = std::tuple<>;
};

template<typename T, typename ...Ts>
struct RemoveFirstElementsImpl<0, T, Ts...>
{
    using type = std::tuple<T, Ts...>;
};

template<int N, typename T, typename ...Ts>
struct RemoveFirstElementsImpl<N, T, Ts...>
{
    using type = typename RemoveFirstElementsImpl<N - 1, Ts...>::type;
};

Live Demo

关于c++ - 整数参数特殊值的模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32570247/

相关文章:

C++ 构建并发送 DNS 数据包

c++17: 一个从未被销毁的临时对象

templates - 为什么 Magento 中的局部变量有一个下划线前缀?

c++ - 如何检查函数中的模板参数是否匹配给定类型别名的特化

C++11:lambda,柯里化(Currying)

multithreading - C++11 使用共享缓冲区创建快速且周期性的通信线程以减慢主处理循环

c++ - 编译时不能包含 Sparkle 框架

c++ - 我怎样才能使模型开发更容易

c++ - 用户定义类型的非类型模板参数

c++ - 复制构造函数不适用于链表?