变异模板部分特化"不是更专业的"在g ++ 7.1.1中

时间:2017-08-25 14:48:43

标签: c++ c++11 variadic-templates template-specialization

我有一个等同于OnActionExecuting()(我们还没有使用C ++ 14)。我还有两个帮助类,删除或添加一个前导号码。

std::integer_sequence

// Sequence type template <typename Type, Type ... Values> struct Sequence { using value_type = Type; }; // Pop a value off of the start of a sequence template <class Class> struct SequencePop; template <typename Type, Type Value, Type ... Values> struct SequencePop<Sequence<Type, Value, Values ...>> { using type = Sequence<Type, Values ...>; }; // Push a value on to the start of a sequence template <class Class, typename Class::value_type Value> struct SequencePush; template <typename Type, Type Value, Type ... Values> struct SequencePush<Sequence<Type, Values ...>, Value> { using type = Sequence<Type, Value, Values ...>; }; 在我尝试过的所有编译器中都被认为是有效的(g ++ 6.4.1,g ++ 7.1.1,clang ++ 4.0.1)。 SequencePop不能用g ++ 7.1.1编译。错误消息如下。

SequencePush

g ++ 7.1.1是否正确拒绝此代码,如果是,那么如何判断test.cpp:24:8: error: partial specialization ‘struct SequencePush<Sequence<Type, Values ...>, Value>’ is not more specialized than [-fpermissive] struct SequencePush<Sequence<Type, Values ...>, Value> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test.cpp:21:8: note: primary template ‘template<class Class, typename Class::value_type Value> struct SequencePush’ struct SequencePush; 不是&#34;更专业&#34;比主要模板?

2 个答案:

答案 0 :(得分:2)

应该是

template <typename Type,
          Type ... Values,
          typename Sequence<Type, Values ...>::value_type Value>
struct SequencePush<Sequence<Type, Values ...>, Value>
{
    using type = Sequence<Type, Value, Values ...>;
};

Demo

来自Class::value_type的{​​{1}}和来自Type的{​​{1}}不是“相关”

答案 1 :(得分:0)

实际上,通用模板SequencePush由一个模板参数(Class)参数化,但您的专业化由两个参数化(TypeValue)。在常规模板中,Type不是参数,因为它是从Class推断出来的。

一个有效但不令人满意的解决方案是:

template <class Class, typename Type, Type Value>                     
struct SequencePush;                                                         

template <typename Type, Type Value, Type ... Values>                        
struct SequencePush<Sequence<Type, Values ...>, Type, Value>                       
{                                                                            
    using type = Sequence<Type, Value, Values ...>;                          
};     

demo