如何将特征作为模板结构的参数传递?
How to pass traits as arguments for a templated struct?
说我有这样的东西
template<typename T1, typename T2>
struct my_struct
{
using type = typename T1<T2>::type;
};
在主函数中,我希望能够编写 using test = typename my_struct<remove_const_t<>, const float>::type;
其中 test 将等于 float,因为 remove_const_t<const float>
returns float.
我怎样才能做到这一点?
您需要模板模板参数。
语法为:
template<template <typename> class C, typename T>
struct my_struct
{
using type = typename C<T>::type;
};
您需要一个 template template parameter,这样您就可以将模板传递给 my_struct
。看起来像
template<template<typename> typename T1, typename T2>
struct my_struct
{
using type = typename T1<T2>::type;
};
然后你会像
一样使用它
using test = typename my_struct<is_integral, float>::type;
说我有这样的东西
template<typename T1, typename T2>
struct my_struct
{
using type = typename T1<T2>::type;
};
在主函数中,我希望能够编写 using test = typename my_struct<remove_const_t<>, const float>::type;
其中 test 将等于 float,因为 remove_const_t<const float>
returns float.
我怎样才能做到这一点?
您需要模板模板参数。
语法为:
template<template <typename> class C, typename T>
struct my_struct
{
using type = typename C<T>::type;
};
您需要一个 template template parameter,这样您就可以将模板传递给 my_struct
。看起来像
template<template<typename> typename T1, typename T2>
struct my_struct
{
using type = typename T1<T2>::type;
};
然后你会像
一样使用它using test = typename my_struct<is_integral, float>::type;