C++ 可变参数模板空包

标签 c++ templates variadic

我正在尝试使用具有以下签名的方法来扩展 std::vector:

template<typename... An>
tuple<An...> get();

我希望它返回 vector 中第一次出现的指定派生类类型。

但是,我在扩展空模板参数包时遇到了一些问题。我找到了这个帖子: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]|

我知道我应该声明 tuple<An... method>以上tuple<A, An...> 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;

最佳答案

您不需要 get 的任何专门化,只需

template<typename... An>
tuple<An...> get()
{
    return tuple_cat(getSingle<An>()...);
}

关于C++ 可变参数模板空包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43941727/

相关文章:

c++ - 如何将 C++ 与 MySQL 连接

c++ - 使用 SSE 的水平最小值和最大值

c++ - 运行时的模板实例化和函数选择

c++ - 处理通用代码中不一致的 typedef

c# - 需要数组的可变参数函数的设计决策是什么?

c++ - std::string 的转换运算符无法处理赋值

c++ - 程序在 Visual Studio 2010 上编译但不在 VS 2015 上编译

c++ - 在 autoconf 中检查仅 header 库

function - 如何处理 Scheme 中未指定数量的参数?

c++ - 如何编写丢弃其参数的通用可变参数 lambda?