我正在为整数参数的模板特化而苦苦挣扎,也许这根本不可能?
我尝试了什么:
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;
};
关于c++ - 整数参数特殊值的模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32570247/