Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在模板参数中查找第一个非空子类型 我试图编写一个C++ Meta函数,它返回给我的第一个非空子类型,用于提供的模板参数。_C++_Templates_Recursion_Variadic Templates_Template Meta Programming - Fatal编程技术网

在模板参数中查找第一个非空子类型 我试图编写一个C++ Meta函数,它返回给我的第一个非空子类型,用于提供的模板参数。

在模板参数中查找第一个非空子类型 我试图编写一个C++ Meta函数,它返回给我的第一个非空子类型,用于提供的模板参数。,c++,templates,recursion,variadic-templates,template-meta-programming,C++,Templates,Recursion,Variadic Templates,Template Meta Programming,例如: struct I{使用subtype=int;}; 结构D{using subtype=double;}; 结构E{using subtype=empty;}; 我正在努力实现: static_断言(std::is_same::value,“第一个非空子类型应该是'int'”; static_assert(std::is_same::value,“第一个非空子类型应为'double'”; static_assert(std::is_same::value,“因为所有子类型都是空的,所以

例如:

struct I{使用subtype=int;};
结构D{using subtype=double;};
结构E{using subtype=empty;};
我正在努力实现:

static_断言(std::is_same::value,“第一个非空子类型应该是'int'”;
static_assert(std::is_same::value,“第一个非空子类型应为'double'”;
static_assert(std::is_same::value,“因为所有子类型都是空的,所以结果是空的”);
我最初的想法是使用
std::conditional\u t
和模板递归:

模板
使用第一个非空子类型=std::conditional\u t<
!std::is_empty::value,
类型名T::子类型,
第一个非空子类型>::类型
然而,我并不完全熟悉为类型别名实现模板递归

有人能帮我指出解决这个问题的正确方向吗


谢谢

我提议如下

// ground case: no more types, so empty
template <typename ...>
struct fnes_helper
 { using type = empty; };

// the first type is T and isn't empy; so T
template <typename T, typename ... Ts>
struct fnes_helper<T, Ts...>
 { using type = T; };

// the first type is empty; so recursion
template <typename ... Ts>
struct fnes_helper<empty, Ts...> : public fnes_helper<Ts...>
 { };

template <typename ... Ts>
using first_non_empty_subtype 
   = typename fnes_helper<typename Ts::subtype...>::type;

为了使递归工作,您需要一些打破递归的东西。在您的示例中没有这样的事情,而且如果不能使用模板别名,那么您不能以任何方式专门化它们。您可以使用subtype=…创建一个只包含
的递归结构,并使用它。如果需要,可以制作指向递归结构的模板别名,以便于使用。感谢您指出这一点!我故意忽略了这一点,因为我不确定它将如何实现。感谢您解释模板别名如何不能被专门化--@max66展示了一个使用递归结构的解决方案。很酷,谢谢!这就是我要找的。感谢您的快速响应和花时间解释解决方案的工作原理。
static_assert(std::is_same<int, first_non_empty_subtype<E,E,I>>{},
              "the first non-empty subtype should be 'int'");
static_assert(std::is_same<double, first_non_empty_subtype<E,D,I>>{},
              "the first non-empty subtype should be 'double'");
static_assert(std::is_same<empty, first_non_empty_subtype<E,E,E>>{},
              "since all subtypes are empty, the result is empty");