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

Type mismatch in variadic template inheritance

我有一个模板,它将参数列表中给出的模板应用于参数包中的所有类型并继承所有这些类型(称为 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) 进行编译。 return 类型应该是 *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 需要一堆 unsigneds:

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)。


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