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

发布于 2024-12-07 22:31:23 字数 1259 浏览 0 评论 0原文

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

对于第一个参数很简单(只需将 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;
}

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

For the 1st argument it is easy (just compare sizeof...(T) with 0), but is there a way to detect the last element?

The example :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱果果 2024-12-14 22:31:23

我不确定这是否是你想要的。但这里有两个名为 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';
}

I'm not positive if this is what you want. But here are two utilities named first and last that take variadic templates and typedef the first and last type respectively:

#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';
}
随心而道 2024-12-14 22:31:23

这是另一组带有便利函数 return_type 的代码,您可以使用它来访问可变模板列表中特定索引处的任何类型...然后您可以调整对 return_type 的调用code> 以便获得第一个和最后一个参数(即,第一个参数位于 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 访问该特定结构实例的可变参数模板中的类型code> 应用于枚举。例如:

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

Here's another set of code with a convenience function return_type that you could use to access any type at a specific index in a varadic template list ... you could then adapt the call to return_type so that you get the first and the last arguments (i.e., the first argument will be at 0, and the last argument will be at 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>();
}

Here's an example of using the convenience function return_type in actual code to determine the Nth template argument in a variadic template:

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

    return 0;
}

In this case, since the int template argument to return_type is 2, you'll get the char type as the output. Any number over 2 will cause an overflow that will create a compile rather than runtime error. As noted, you could adapt it so that it's wrapped inside a function in a structure that will allow you to access the types in the variadic template for that specific structure instance using the sizeof...(TypeList) - 1 applied to an enum. For instance:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文