如何确定元组是否包含类型? [英] How do I find out if a tuple contains a type?

查看:100
本文介绍了如何确定元组是否包含类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想从非唯一类型的一些序列创建一个具有唯一类型的编译时异构容器。为了做到这一点,我需要迭代源类型(某种 tuple ),并检查每个类型是否已经存在于我的唯一元组中。

Suppose I want to create a compile-time heterogenous container of unique types from some sequence of non-unique types. In order to do this I need to iterate over the source type (some kind of tuple) and check whether each type already exists in my "unique" tuple.

我的问题是:如何检查元组(或 boost :: fusion 容器)是否包含类型

My question is: How can I check whether a tuple (or a boost::fusion container) contains a type?

我可以使用STL或 boost

推荐答案

#include <tuple>
#include <type_traits>

template <typename T, typename Tuple>
struct has_type;

template <typename T>
struct has_type<T, std::tuple<>> : std::false_type {};

template <typename T, typename U, typename... Ts>
struct has_type<T, std::tuple<U, Ts...>> : has_type<T, std::tuple<Ts...>> {};

template <typename T, typename... Ts>
struct has_type<T, std::tuple<T, Ts...>> : std::true_type {};

DEMO

和一个额外的别名,如果trait本身应 std :: true_type std :: false_type

And an additional alias, if the trait itself should be std::true_type or std::false_type :

template <typename T, typename Tuple>
using tuple_contains_type = typename has_type<T, Tuple>::type;

这篇关于如何确定元组是否包含类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆