本文介绍了如何从元组C ++过滤重复类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个如何从元组中过滤重复类型?

How does one filter duplicate types from a tuple?

例如:

using Tuple = std::tuple<int, double, int, double, std::string, std::string>
using FilteredTuple = without_duplicates<Tuple>;

其中的without_duplicates的实现方式是生成以下FilteredTuple类型:

In which without_duplicates is implemented in such a way that it generates the following FilteredTuple type:

std::tuple<int, double, std::string>

推荐答案

这项应有的工作:

template <class Haystack, class Needle>
struct contains;

template <class Car, class... Cdr, class Needle>
struct contains<std::tuple<Car, Cdr...>, Needle> : contains<std::tuple<Cdr...>, Needle>
{};

template <class... Cdr, class Needle>
struct contains<std::tuple<Needle, Cdr...>, Needle> : std::true_type
{};

template <class Needle>
struct contains<std::tuple<>, Needle> : std::false_type
{};



template <class Out, class In>
struct filter;

template <class... Out, class InCar, class... InCdr>
struct filter<std::tuple<Out...>, std::tuple<InCar, InCdr...>>
{
  using type = typename std::conditional<
    contains<std::tuple<Out...>, InCar>::value
    , typename filter<std::tuple<Out...>, std::tuple<InCdr...>>::type
    , typename filter<std::tuple<Out..., InCar>, std::tuple<InCdr...>>::type
  >::type;
};

template <class Out>
struct filter<Out, std::tuple<>>
{
  using type = Out;
};


template <class T>
using without_duplicates = typename filter<std::tuple<>, T>::type;

[实时示例]

它通过迭代构造输出元组来工作.在添加每种类型之前,请检查(使用谓词contains)它是否已存在于输出元组中.如果没有,则添加(std::conditional的"else"分支),否则不添加(std::conditional的"then"分支).

It works by iteratively constructing the output tuple. Before each type is added, check (using the predicate contains) whether it's already in the output tuple or not. If not, it's added (the "else" branch of std::conditional), otherwise it's not added (the "then" branch of std::conditional).

这篇关于如何从元组C ++过滤重复类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:18