我正在尝试使用方法扩展std :: vector类 签名:
template<typename... An>
tuple<An...> get();
它应该在向量中返回第一次出现的指定派生类类型。
我在扩展空模板包时遇到了一些问题,我找到了这个帖子:How to match empty arguments pack in variadic template所以我修改了我的类,现在看起来像这样:
template<typename T>
class AggregationVector : public vector<T>
{
public:
template<typename A>
tuple<A> getSingle()
{
for (auto i = vector<T>::begin(); i != vector<T>::end(); i++)
{
A element = dynamic_cast<A>(*i);
if (element != nullptr) return make_tuple(element);
}
return make_tuple(nullptr);
}
template<typename A, typename... An>
tuple<A, An...> getHelper()
{
return tuple_cat(getSingle<A>(), get<An...>());
}
template<typename... An>
tuple<An...> get()
{
return getHelper<An...>();
}
template<>
tuple<> get<>()
{
return make_tuple();
}
};
不幸的是我仍然遇到错误,我不知道如何解决它们:
Line 29: explicit specialization in non-namespace scope 'class AggregationVector<T>'|
Line 30: template-id 'get<>' in declaration of primary template|
In instantiation of 'std::tuple<_Args1 ...> AggregationVector<T>::get() [with An = {}; T = AggregationVectorTest::Base*]':|
Line 20: required from 'std::tuple<A, An ...> AggregationVector<T>::getHelper() [with A = AggregationVectorTest::DoubleDerived1*; An = {}; T = AggregationVectorTest::Base*]'|
Line 26: required from 'std::tuple<_Args1 ...> AggregationVector<T>::get() [with An = {AggregationVectorTest::DoubleDerived1*}; T = AggregationVectorTest::Base*]'|
Line 20: required from 'std::tuple<A, An ...> AggregationVector<T>::getHelper() [with A = AggregationVectorTest::Base*; An = {AggregationVectorTest::DoubleDerived1*}; T = AggregationVectorTest::Base*]'|
Line 26: required from 'std::tuple<_Args1 ...> AggregationVector<T>::get() [with An = {AggregationVectorTest::Base*, AggregationVectorTest::DoubleDerived1*}; T = AggregationVectorTest::Base*]'|
Line (this error points to line starting with "tuple<Base*, DoubleDerived1*> tup1" from example code): required from here|
Line 26: error: no matching function for call to 'AggregationVector<AggregationVectorTest::Base*>::getHelper()'|
Line 26: note: candidate is:|
Line 18: note: template<class A, class ... An> std::tuple<A, An ...> AggregationVector<T>::getHelper() [with A = A; An = {An ...}; T = AggregationVectorTest::Base*]|
Line 18: note: template argument deduction/substitution failed:|
Line 26: note: couldn't deduce template parameter 'A'|
In member function 'std::tuple<_Args1 ...> AggregationVector<T>::get() [with An = {}; T = AggregationVectorTest::Base*]':|
Line 27: warning: control reaches end of non-void function [-Wreturn-type]|
我知道我应该将元组的声明放在元组getHelper()之上,但是它会给我一个错误,即get()方法不能重载
'template<class T> template<class ... An> std::tuple<_Args1 ...> AggregationVector<T>::get()' cannot be overloaded|
with 'template<class T> template<class ... An> std::tuple<_Args1 ...> AggregationVector<T>::get()'|
我测试它的示例代码如下:
// declaration of few polymorphic classes
class Base
{
public:
int Value;
Base(int v = 0) { Value = v; }
virtual void write()
{
cout << "Base " << Value << endl;
}
virtual ~Base();
};
class Derived1 : public Base
{
public:
Derived1(int v = 0) : Base::Base(v) { }
virtual void write()
{
cout << "Derived1 " << Value << endl;
}
};
class DoubleDerived1 : public Derived1
{
public:
DoubleDerived1(int v = 0) : Derived1::Derived1(v) { }
virtual void write()
{
cout << "DoubleDerived1 " << Value << endl;
}
};
class Derived2 : public Base
{
public:
Derived2(int v = 0) : Base::Base(v) { }
virtual void write()
{
cout << "Derived2 " << Value << endl;
}
};
//test implementation
Base a1(1);
Base a2(2);
Derived1 b3(3);
DoubleDerived1 bb4(4);
Derived2 c5(5);
Derived2 c6(6);
AggregationVector<Base*> vec {&a1, &a2, &b3, &bb4, &c5, &c6};
tuple<Base*, DoubleDerived1*> tup1 = vec.get<Base*, DoubleDerived1*>();
tuple<Derived1*, DoubleDerived1*, Derived2*> tup2 = vec.get<Derived1*, DoubleDerived1*, Derived2*>();
cout << "Test Tuple1 result: " << (get<0>(tup1)->Value == 1 && get<1>(tup1)->Value == 4) << endl;
cout << "Test Tuple2 result: " << (get<0>(tup2)->Value == 3 && get<1>(tup2)->Value == 4 && get<2>(tup2)->Value == 5) << endl;
答案 0 :(得分:4)
您不需要get
的任何专精,只需
template<typename... An>
tuple<An...> get()
{
return tuple_cat(getSingle<An>()...);
}