如何从模板参数包中删除类型? [英] How can a type be removed from a template parameter pack?

查看:42
本文介绍了如何从模板参数包中删除类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种从模板参数包中删除类型的方法(现在所有发生的).最终结果将是一个看起来像这样的结构:

I'm searching for a way to remove (let's say for now all occurences of) a type from a template parameter pack. The end result would be a struct that looked like this :

template<typename T, typename...Ts>
struct RemoveT
{
    using type = /* a new type out of Ts that does not contain T */
}

让我们说,边际大小写的 RemoveT< int,int> 将通过返回 void 来处理(以下代码中未处理).我的初始设计如下:

Let's say that the marginal case RemoveT<int, int> would be handled by returning void (not handled in the code that follows). My initial design looks like this:

// --------------------------------------------------------------
// 1. A "way" of typedefing variadic number of types ------------
template<typename...Ts>
struct pack { 
    using type = Ts; 
};
// --------------------------------------------------------------

// --------------------------------------------------------------
template<typename T, typename...Ts> struct RemoveT;

template<typename T, typename T1, typename...Ts>
struct RemoveT {
    using type = typename pack<T1, typename RemoveT<T, Ts...>::type>::type;
};

template<typename T, typename T1>
struct RemoveT<T, T1> { 
    using type = T1; 
};

template<typename T, typename...Ts>
struct RemoveT<T, T, Ts...> {
    using type = typename RemoveT<Ts...>::type;
};
// --------------------------------------------------------------

现在我什至无法开始测试此代码,因为 pack结构无效的C ++

Now I can't even begin to test this code because the pack structure is not valid C++

以防万一,这对于回答问题很有帮助,但在解决该问题时还有一些其他建议

Just in case this is helpfull for an answer, some other thoughs on solving it

  • 有人可能会说 pack 根本没有用.相反,我们可以绕过 RemoveT 结构,创建一个仅包含所需类型的新的 RemoveT .然后问题就转化为从结构中提取类型
  • 我们可以创建模仿 typelists 行为的类型对,并对此采取更递归的方法.
  • One could argue that pack is not even useful at all. We could instead move around the RemoveT structure, creating a new RemoveT that only contains the types needed. The problem then transforms in extracting the types out of the struct
  • We could create type pairs that mimic the behaviour of typelists and take a more recursive approach on this.

对于可变参数类型 Ts 和类型 T :我可以在 Ts 之外创建 Us 省略 T 吗?

For variadic types Ts and a type T: Can I create Us out of Ts ommiting T ?

推荐答案

以下内容提供了一种非递归的直接方法,可从 Ts ... 中删除 T 像Jarod42的解决方案一样,产生 std :: tuple< Us ...> ,但无需使用 typename ... :: type :

The following provides a non-recursive and direct way to remove T from Ts... and, like Jarod42's solutions, yields a std::tuple<Us...> but without the need to use typename ...::type:

#include <tuple>
#include <type_traits>

template<typename...Ts>
using tuple_cat_t = decltype(std::tuple_cat(std::declval<Ts>()...));

template<typename T, typename...Ts>
using remove_t = tuple_cat_t<
    typename std::conditional<
        std::is_same<T, Ts>::value,
        std::tuple<>,
        std::tuple<Ts>
    >::type...
>;


int main()
{
    static_assert(std::is_same<
        remove_t<int, int, char, int, float, int>,
        std::tuple<char, float>
    >::value, "Oops");
}

实时示例

这篇关于如何从模板参数包中删除类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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