![](/img/trans.png)
[英]Polymorphism, variadic template inheritance, slicing, boost::any type cast
[英]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)進行編譯,聲稱無效的引用初始化。 返回類型應該是* 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;
和一個用於構建整數序列(序列):
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
s:
template <unsigned...>
struct Seq { typedef int value_type; };
但你專注於它需要一堆int
:
template<template<int, typename> class Template,
int firstIndex, int... indexes, // <==
typename First, typename... Args>
struct _Map<Template, Seq<firstIndex, indexes...>, First, Args...>
那些需要排隊。 Clang接受代碼,但這是一個鏗鏘的bug( #28010 )。
正如我在評論中提到的,您使用的是標准保留的許多標識符。 保留任何以下划線開頭並后跟大寫字母的單詞:不要使用它們!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.