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

How to detect the first and the last argument in the variadic templates?

本文关键字:第一个 参数 最后一个 何检测 检测      更新时间:2023-10-16

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

对于第一个参数,这很容易(只是比较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;
}

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

#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';
}

这是另一组带有方便函数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;
}

在本例中,由于return_typeint模板参数是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;
}