如何使用数字序列解压可变参数模板参数?

发布于 2024-12-13 20:09:22 字数 532 浏览 2 评论 0原文

如何(或者是否可以)使用数字序列解压参数包?例如,

template <typename C, typename... T>
C* init_from_tuple(bp::tuple tpl)
{
   return new C{bp::extract<T>("magic"(tpl))...}; // <--
}

<-- 行应扩展为

   return new C{bp::extract<T0>(tpl[0]),
                bp::extract<T1>(tpl[1]),
                .....
                bp::extract<Tn>(tpl[n])};

n == sizeof...(T) - 1

目的是为 Boost.Python 创建一个 __init__ 函数,它接受具有预定义类型的元组。

How to (or is it possible to) unpack a parameter pack with a numeric sequence? For example,

template <typename C, typename... T>
C* init_from_tuple(bp::tuple tpl)
{
   return new C{bp::extract<T>("magic"(tpl))...}; // <--
}

which the <-- line should expand to

   return new C{bp::extract<T0>(tpl[0]),
                bp::extract<T1>(tpl[1]),
                .....
                bp::extract<Tn>(tpl[n])};

where n == sizeof...(T) - 1.

The purpose is to create a __init__ function for Boost.Python which accepts a tuple with predefined types.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆离笙 2024-12-20 20:09:22

实际上,解包操作可以同时针对两个不同的参数包(我认为它们需要具有相同的长度)。这里我们想要一组类型和一组数字。

类似于:

template <typename C, typename... T, size_t... N>
C* init_from_tuple_impl(bp::tuple tpl) {
  return new C{ bp::extract<T>(tpl[N])... };
}

我们“只是”需要生成索引包:

template <size_t... N> struct Collection {};

template <typename C> struct ExtendCollection;

template <size_t... N>
struct ExtendCollection< Collection<N...> > {
  typedef Collection<N..., sizeof...(N)> type;
};

template <typename... T>
struct GenerateCollection;

template <>
struct GenerateCollection<> { typedef Collection<> type; };

template <typename T0, typename... T>
struct GenerateCollection<T0, T...> {
  typedef typename ExtendCollection<
    typename GenerateCollection<T...>::type
  >::type type;
};

然后使用它:

template <typename C, typename... T, size_t... N>
C* init_from_tuple_impl(bp::tuple tpl, Collection<N...>) {
  return new C { bp::extract<T>(tpl[N])... };
}

template <typename C, typename... T>
C* init_from_tuple(bp::tuple tpl) {
  typename GenerateCollection<T...>::type collection;
  return init_from_tuple_impl<C, T...>(tpl, collection);
}

Ideone 上进行操作。

我们可以通过在 init_from_tuple_impl 的实现中犯下一个“错误”来见证正确性(例如删除 new):

template <typename C, typename... T, size_t... N>
C* init_from_tuple_impl(bp::tuple tpl, Collection<N...>) {
  return C { bp::extract<T>(tpl[N])... };
}

Ideone

prog.cpp: In function 'C* init_from_tuple_impl(bp::tuple, Collection<N ...>)
[with
    C = bp::Object,
    T = {int, float, char},
    unsigned int ...N = {0u, 1u, 2u},
    bp::tuple = std::basic_string<char>
]':

正是我们想要的:)

Actually, it is possible for the unpack operations to target two different packs of parameters at once (I think they need be of equal length). Here we would like a pack of types, and a pack of numbers.

Something akin to:

template <typename C, typename... T, size_t... N>
C* init_from_tuple_impl(bp::tuple tpl) {
  return new C{ bp::extract<T>(tpl[N])... };
}

We "just" need to generate the pack of indices:

template <size_t... N> struct Collection {};

template <typename C> struct ExtendCollection;

template <size_t... N>
struct ExtendCollection< Collection<N...> > {
  typedef Collection<N..., sizeof...(N)> type;
};

template <typename... T>
struct GenerateCollection;

template <>
struct GenerateCollection<> { typedef Collection<> type; };

template <typename T0, typename... T>
struct GenerateCollection<T0, T...> {
  typedef typename ExtendCollection<
    typename GenerateCollection<T...>::type
  >::type type;
};

And then use it:

template <typename C, typename... T, size_t... N>
C* init_from_tuple_impl(bp::tuple tpl, Collection<N...>) {
  return new C { bp::extract<T>(tpl[N])... };
}

template <typename C, typename... T>
C* init_from_tuple(bp::tuple tpl) {
  typename GenerateCollection<T...>::type collection;
  return init_from_tuple_impl<C, T...>(tpl, collection);
}

In action at Ideone.

We can witness the correctness by making a "mistake" in the implementation of init_from_tuple_impl (remove the new for example):

template <typename C, typename... T, size_t... N>
C* init_from_tuple_impl(bp::tuple tpl, Collection<N...>) {
  return C { bp::extract<T>(tpl[N])... };
}

In action at Ideone:

prog.cpp: In function 'C* init_from_tuple_impl(bp::tuple, Collection<N ...>)
[with
    C = bp::Object,
    T = {int, float, char},
    unsigned int ...N = {0u, 1u, 2u},
    bp::tuple = std::basic_string<char>
]':

Exactly what we wanted :)

×眷恋的温暖 2024-12-20 20:09:22

如果您首先将参数提取到它们自己的包中,然后调用构造函数,这是可能的。它还远未完成,但您已经了解了总体思路:

template <typename C, int N, typename... T>
C* init_from_tuple(bp::tuple tpl, T... args) // enable_if N == sizeof...(T)
{
    return new C{args...};
}

template <typename C, int N, typename T0, typename... T>
C* init_from_tuple(bp::tuple tpl, T... args) // enable_if N != sizeof...(T)
{
    return init_from_tuple<C, N + 1>(tpl, args, bp::extract<T0>(tpl[N]));
}

template <typename C, typename... T>
C* init_from_tuple(bp::tuple tpl, T... args)
{
    return init_from_tuple<C, 0, T...>(tpl, args);
}

使用 boost 的 enable_if 使指示的位置仅在某些情况下启用,并且模板参数可能需要一些更改,但这只是一个开始。

Its possible if you first extract the parameters into their own pack and then call the constructor. its far from finished, but you get the general idea:

template <typename C, int N, typename... T>
C* init_from_tuple(bp::tuple tpl, T... args) // enable_if N == sizeof...(T)
{
    return new C{args...};
}

template <typename C, int N, typename T0, typename... T>
C* init_from_tuple(bp::tuple tpl, T... args) // enable_if N != sizeof...(T)
{
    return init_from_tuple<C, N + 1>(tpl, args, bp::extract<T0>(tpl[N]));
}

template <typename C, typename... T>
C* init_from_tuple(bp::tuple tpl, T... args)
{
    return init_from_tuple<C, 0, T...>(tpl, args);
}

Use boost's enable_if to make the places indicated only enabled in some cases, and probably the template arguments need some changes, but this is a start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文