我有一个等同于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;比主要模板?
答案 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 ...>;
};
来自Class::value_type
的{{1}}和来自Type
的{{1}}不是“相关”。
答案 1 :(得分:0)
实际上,通用模板SequencePush
由一个模板参数(Class
)参数化,但您的专业化由两个参数化(Type
和Value
)。在常规模板中,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 ...>;
};