实现具有常量正确性的可变参数 zip 函数

发布于 2024-11-19 02:27:16 字数 1372 浏览 3 评论 0 原文

我正在尝试实现 zip 功能。 zip 的参数均是 wrapped,其中 Ti 因参数而异。

zip 采用这些 wrapped 并生成 wrapped> >,或者换句话说,对其参数的引用的包装元组。引用应该保留const性质。

这是我对带有一个参数的 zip 的第一次尝试,它通常不起作用:

#include <utility>
#include <tuple>

// implement forward_as_tuple as it is missing on my system
namespace ns
{

template<typename... Types>
  std::tuple<Types&&...>
    forward_as_tuple(Types&&... t)
{
  return std::tuple<Types&&...>(std::forward<Types>(t)...);
}

}

template<typename T>
  struct wrapped
{
  wrapped(T &&x)
    : m_x(std::forward<T>(x))
  {}

  T m_x;
};

template<typename T>
  wrapped<std::tuple<T&&>>
    zip(wrapped<T> &&x)
{
  auto t = ns::forward_as_tuple(std::forward<T>(x.m_x));
  return wrapped<std::tuple<T&&>>(t);
}

int main()
{
  wrapped<int> w1(13);

  wrapped<int> &ref_w1 = w1;

  // OK
  zip(ref_w1);

  const wrapped<int> &cref_w1 = w1;

  // XXX won't compile when passing a const reference
  zip(cref_w1);

  return 0;
}

有没有办法用单个版本的 zip 实现通用的可变情况?

I'm trying to implement a zip function. zip's parameters are each wrapped<Ti>, where Ti varies from parameter to parameter.

zip takes these wrapped<Ti>s and produces a wrapped<tuple<T1&,T2&,...TN&>>, or in other words a wrapped tuple of references to its parameters. The references should preserve const-ness.

Here's my first stab at zip with one parameter, which doesn't work in general:

#include <utility>
#include <tuple>

// implement forward_as_tuple as it is missing on my system
namespace ns
{

template<typename... Types>
  std::tuple<Types&&...>
    forward_as_tuple(Types&&... t)
{
  return std::tuple<Types&&...>(std::forward<Types>(t)...);
}

}

template<typename T>
  struct wrapped
{
  wrapped(T &&x)
    : m_x(std::forward<T>(x))
  {}

  T m_x;
};

template<typename T>
  wrapped<std::tuple<T&&>>
    zip(wrapped<T> &&x)
{
  auto t = ns::forward_as_tuple(std::forward<T>(x.m_x));
  return wrapped<std::tuple<T&&>>(t);
}

int main()
{
  wrapped<int> w1(13);

  wrapped<int> &ref_w1 = w1;

  // OK
  zip(ref_w1);

  const wrapped<int> &cref_w1 = w1;

  // XXX won't compile when passing a const reference
  zip(cref_w1);

  return 0;
}

Is there a way to implement the general, variadic case with a single version of zip?

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

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

发布评论

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

评论(3

眼波传意 2024-11-26 02:27:16

诚然,我没有处理可变参数模板的 C++0x 编译器,所以我无法测试它。但这可能会起作用。

template<typename T>
    struct wrapped
{
    wrapped(T &&x)
    : m_x(std::forward<T>(x))
    {}

    typedef T type;

    T m_x;
};

template<typename... Types>
    wrapped<std::tuple<Types&&...>> zip(wrapped<Types>&&... x)
{
    return wrapped<std::tuple<Types&&...>>(std::tuple<Types&&...>(std::forward<Types>(x.m_x)...));
}

我不完全确定像这样调用 zip 是否合法:

zip(wrapped<T1>(value1), wrapped<T2>(value2));

您可能必须明确限定该调用:

zip<T1, T2>(wrapped<T1>(value1), wrapped<T2>(value2));

Admittedly, I don't have a C++0x compiler that handles variadic templates, so I can't test it. But this might do the trick.

template<typename T>
    struct wrapped
{
    wrapped(T &&x)
    : m_x(std::forward<T>(x))
    {}

    typedef T type;

    T m_x;
};

template<typename... Types>
    wrapped<std::tuple<Types&&...>> zip(wrapped<Types>&&... x)
{
    return wrapped<std::tuple<Types&&...>>(std::tuple<Types&&...>(std::forward<Types>(x.m_x)...));
}

I'm not entirely sure if it is legal to call zip like this:

zip(wrapped<T1>(value1), wrapped<T2>(value2));

You may have to explicitly qualify the call:

zip<T1, T2>(wrapped<T1>(value1), wrapped<T2>(value2));
纵性 2024-11-26 02:27:16

这是我得出的解决方案:

#include <utility>
#include <tuple>
#include <cassert>

template<typename T>
  struct wrapped
{
  wrapped(T &&x)
    : m_x(std::forward<T>(x))
  {}

  T m_x;
};

template<typename Tuple>
  wrapped<Tuple> make_wrapped_tuple(Tuple &&x)
{
  return wrapped<Tuple>(std::forward<Tuple>(x));
}

template<typename... WrappedTypes>
  decltype(make_wrapped_tuple(std::forward_as_tuple(std::declval<WrappedTypes>().m_x...)))
    zip(WrappedTypes&&... x)
{
  return make_wrapped_tuple(std::forward_as_tuple(x.m_x...));
}

int main()
{
  wrapped<int> w1(1);
  wrapped<int> w2(2);
  wrapped<int> w3(3);
  wrapped<int> w4(4);

  auto z1 = zip(w1,w2,w3,w4);

  z1.m_x = std::make_tuple(11,22,33,44);

  assert(w1.m_x == 11);
  assert(w2.m_x == 22);
  assert(w3.m_x == 33);
  assert(w4.m_x == 44);

  const wrapped<int> &cref_w1 = w1;

  auto z2 = zip(cref_w1, w2, w3, w4);

  // does not compile, as desired
  // z2.m_x = std::make_tuple(111,222,333,444);

  return 0;
}

使用 zip 采用 WrappedTypes... 而不是 wrapped... 并不令人满意解决方案,但它有效。

Here's the solution I arrived at:

#include <utility>
#include <tuple>
#include <cassert>

template<typename T>
  struct wrapped
{
  wrapped(T &&x)
    : m_x(std::forward<T>(x))
  {}

  T m_x;
};

template<typename Tuple>
  wrapped<Tuple> make_wrapped_tuple(Tuple &&x)
{
  return wrapped<Tuple>(std::forward<Tuple>(x));
}

template<typename... WrappedTypes>
  decltype(make_wrapped_tuple(std::forward_as_tuple(std::declval<WrappedTypes>().m_x...)))
    zip(WrappedTypes&&... x)
{
  return make_wrapped_tuple(std::forward_as_tuple(x.m_x...));
}

int main()
{
  wrapped<int> w1(1);
  wrapped<int> w2(2);
  wrapped<int> w3(3);
  wrapped<int> w4(4);

  auto z1 = zip(w1,w2,w3,w4);

  z1.m_x = std::make_tuple(11,22,33,44);

  assert(w1.m_x == 11);
  assert(w2.m_x == 22);
  assert(w3.m_x == 33);
  assert(w4.m_x == 44);

  const wrapped<int> &cref_w1 = w1;

  auto z2 = zip(cref_w1, w2, w3, w4);

  // does not compile, as desired
  // z2.m_x = std::make_tuple(111,222,333,444);

  return 0;
}

Having zip take WrappedTypes... instead of wrapped<T>... isn't as satisfying a solution, but it works.

眼波传意 2024-11-26 02:27:16
template<typename T>
    struct wrapped
{
    wrapped(T &&x)
    : m_x(std::forward<T>(x))
    {}

    typedef T type;

    T m_x;
};

template<typename... Types>
    wrapped<std::tuple<T&&...>> zip(wrapped<Types>... &&x)
{
    return G+
template<typename T>
    struct wrapped
{
    wrapped(T &&x)
    : m_x(std::forward<T>(x))
    {}

    typedef T type;

    T m_x;
};

template<typename... Types>
    wrapped<std::tuple<T&&...>> zip(wrapped<Types>... &&x)
{
    return G+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文