17

如何检测可变参数模板中的第一个和最后一个参数?

对于第一个参数很容易(只需sizeof...(T)与 0 比较),但是有没有办法检测最后一个元素?

这个例子 :

#include <iostream>
#include <typeinfo>

template < class... T >
struct A
{
    int foo(int k){ return k; };
};

template < class T1, class... T >
struct A< T1, T... >
{
    A() :a()
    {
        std::cout<<"A  i="<<sizeof...(T)<<std::endl
                 <<"   a type = " << typeid(T1).name()<<std::endl;
    }

    int foo(int k){ return anotherA.foo( a.foo(k) ); };

    T1 a;
    A< T... > anotherA;
};

struct B1
{
    B1(){ std::cout<<"b1"<<std::endl; };
    int foo(int k){ std::cout<<"b1::foo() k="<<k<<std::endl; return k+1; };
};
struct B2
{
    B2(){ std::cout<<"b2"<<std::endl; };
    int foo(int k){ std::cout<<"b2::foo() k="<<k<<std::endl; return k+2; };
};
struct B3
{
    B3(){ std::cout<<"b3"<<std::endl; };
    int foo(int k){ std::cout<<"b3::foo() k="<<k<<std::endl; return k+3; };
};

int main ()
{
    A< B3, B2, B1 > a;

    std::cout<<"the value is "
             <<a.foo(5)
             << std::endl;
}
4

2 回答 2

26

如果这是你想要的,我不肯定。但是这里有两个名为的实用程序firstlast它们分别采用可变参数模板和 typedef 第一个和最后一个类型:

#include <iostream>
#include <typeinfo>

template <class T1, class ...T>
struct first
{
    typedef T1 type;
};

template <class T1, class ...T>
struct last
{
    typedef typename last<T...>::type type;
};

template <class T1>
struct last<T1>
{
    typedef T1 type;
};

template <class ...T>
struct A
{
    typedef typename first<T...>::type first;
    typedef typename last<T...>::type  last;
};

struct B1 {};
struct B2 {};
struct B3 {};

int main()
{
    typedef A<B1, B2, B3> T;
    std::cout << typeid(T::first).name() << '\n';
    std::cout << typeid(T::last).name() << '\n';
}
于 2011-10-05T16:08:17.403 回答
3

这是另一组带有便利函数的代码return_type,您可以使用它来访问可变参数模板列表中特定索引处的任何类型......然后您可以调整调用,return_type以便获得第一个和最后一个参数(即第一个参数为 0,最后一个参数为sizeof...(TypeList)):

template<typename T>
struct type_id_struct
{
    typedef T type;
    T object_instance;
};

template<int N, typename... TypeList>
struct reduce {};

template<int N, typename T1, typename... TypeList>
struct reduce<N, T1, TypeList...>
{
    typedef typename reduce<N - 1, TypeList... >::type type;
};

template<typename T1, typename... TypeList>
struct reduce<0, T1, TypeList...>
{
    typedef T1 type;
};

//convenience function
template<int N, typename... TypeList>
type_id_struct<typename reduce<N, TypeList...>::type> return_type()
{
        return type_id_struct<typename reduce<N, TypeList...>::type>();
}

下面是return_type在实际代码中使用便利函数来确定可变参数模板中的第 N 个模板参数的示例:

int main()
{
    auto type_returned = return_type<2, int, double, char>();
    std::cout << typeid(type_returned.object_instance).name() << std::endl;

    return 0;
}

在这种情况下,由于int模板参数return_typeis 2,您将获得char类型作为输出。任何数字超过2将导致溢出,这将创建编译而不是运行时错误。如前所述,您可以对其进行调整,以便将其包装在结构中的函数中,该结构将允许您使用sizeof...(TypeList) - 1应用于枚举的特定结构实例访问可变参数模板中的类型。例如:

template<typename... TypeList>
struct an_object
{
    enum { first = 0, last = (sizeof...(TypeList) - 1) };

    template<int N>
    auto wrapper() -> decltype(return_type<N, TypeList...>())
    {
            return return_type<N, TypeList...>();
    }
};

//...more code

int main()
{
    an_object<int, double, char> a;

    auto r_type1 = a.wrapper<an_object<int, double, char>::first>();
    std::cout << typeid(r_type1.object_instance).name() << std::endl;

    auto r_type2 = a.wrapper<an_object<int, double, char>::last>();
    std::cout << typeid(r_type2.object_instance).name() << std::endl;

    return 0;
}
于 2011-10-05T18:01:36.267 回答