如何将特征作为模板结构的参数传递



假设我有这样的

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>返回float。

我怎样才能做到这一点?

您需要一个模板模板参数,以便将模板传递给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<template <typename> class C, typename T>
struct my_struct
{
using type = typename C<T>::type;
};

最新更新