gpt4 book ai didi

c++ - g++ 7.1.1 中的可变参数模板部分特化 "not more specialised"

转载 作者:太空狗 更新时间:2023-10-29 21:34:37 25 4
gpt4 key购买 nike

我有一个等同于 std::integer_sequence 的函数(我们还没有使用 C++14)。我还有两个辅助类,用于删除或添加前导数字。

// 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 ...>;
};

SequencePop 在我尝试过的所有编译器(g++ 6.4.1、g++ 7.1.1、clang++ 4.0.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;

g++ 7.1.1 拒绝此代码是否正确?如果是,如何判断 SequencePush 不比主模板“更专业”?

最佳答案

应该是

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_typeType来自 Sequence<Type, Values ...>“相关”

关于c++ - g++ 7.1.1 中的可变参数模板部分特化 "not more specialised",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45884065/

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