gpt4 book ai didi

c++ - 可变参数模板继承中的类型不匹配

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

我有一个模板,它将参数列表中给出的模板应用于参数包中的所有类型并继承所有这些类型(称为 ApplyOnPack):

template<template<int, typename> class Template, typename Seq, typename... Args>
struct _Map {};

template<template<int, typename> class Template,
int firstIndex, int... indexes,
typename First, typename... Args>
struct _Map<Template, Seq<firstIndex, indexes...>, First, Args...>
: Template<firstIndex, First>,
_Map<Template, Seq<indexes...>, Args...>
{};

template<template<int, typename> class Template, typename... Args>
struct ApplyOnPack : _Map<Template, Sequence<sizeof...(Args)>, Args... >
{
template <int I>
struct Base {
typedef Template<I, GetNthParameter<I, Args...> > Type;
};

template <int I>
typename Base<I>::Type& base() { return *this; }
};

问题是,最后一个 base() 方法将无法通过 gcc (4.9.2) 声称无效引用初始化进行编译。返回类型应该是 *this 类型的基类之一,那么可能是什么问题呢?或者如何修改代码以使其可编译?代码在 msvc (2013) 下编译和运行。

我用下面的例子测试了它:

template <int i, typename T>
struct Part {
void foo() {}
};

template <typename ... T>
struct Foo : ApplyOnPack<Part, T...>
{
void bar() { this->template base<0>().foo(); }
};

typedef Foo<int, bool> MyFoo;

int main() {
MyFoo myFoo;
myFoo.bar();
}

哪个 gcc 失败了:

a.cpp: In instantiation of ‘typename ApplyOnPack<Template, Args>::Base<I>::Type& ApplyOnPack<Template, Args>::base() [with int I = 0; Template = Part; Args = {int, bool}; typename ApplyOnPack<Template, Args>::Base<I>::Type = Part<0, int>]’:

a.cpp:62:15: required from ‘void Foo<T>::bar() [with T = {int, bool}]’
a.cpp:69:12: required from here
a.cpp:51:43: error: invalid initialization of reference of type ‘ApplyOnPack<Part, int, bool>::Base<0>::Type& {aka Part<0, int>&}’ from expression of type ‘ApplyOnPack<Part, int, bool>’
typename Base<I>::Type& base() { return *this; }

遵循上面使用的附加模板:用于从参数包中提取第 n 个参数的模板(GetNthParameter):

template <int I, class... T>
struct _GetNthParameter;

template <int I, class Head, class... Tail>
struct _GetNthParameter<I, Head, Tail...>
: _GetNthParameter<I-1, Tail...>{};

template <class Head, class... Tail>
struct _GetNthParameter<0, Head, Tail...> {
typedef Head Type;
};

template<int index, typename... Types>
using GetNthParameter = typename _GetNthParameter<index, Types...>::Type;

还有一个用于构建整数序列(Sequence):

template<unsigned...>
struct Seq { typedef int value_type; };

template<unsigned max, unsigned... numbers>
struct _ExpandSeq : _ExpandSeq<max-1, max-1, numbers...> {};

template<unsigned... numbers>
struct _ExpandSeq<0, numbers...> {
typedef Seq<numbers...> type;
};

template<unsigned max>
using Sequence = typename _ExpandSeq<max>::type;

最佳答案

您的类型不匹配。 Seq 需要一堆 unsigned:

template <unsigned...>
struct Seq { typedef int value_type; };

但是你专注于它需要一堆 ints:

template<template<int, typename> class Template,
int firstIndex, int... indexes, // <==
typename First, typename... Args>
struct _Map<Template, Seq<firstIndex, indexes...>, First, Args...>

需要排队的。 Clang 接受代码,但这是一个 clang 错误 ( #28010 )。


正如我在评论中提到的,您使用的是标准保留的许多标识符。任何以下划线开头且后跟大写字母的单词都是保留的:不要使用它们!

关于c++ - 可变参数模板继承中的类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37643751/

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