C++0X 中的解包、函数应用和打包元组

发布于 2024-11-30 08:15:43 字数 949 浏览 2 评论 0原文

在不使用 Boost 的情况下,在以下代码中编写 readvals 函数的最佳方法是什么?基本上,它应该获取一个元组,调用其元素的特定函数并再次将生成的结果作为元组返回。

是否有基于 C++0X 的 Functor 元组定义库?

template <class T>
struct A
{
    A(T _val):val(_val){}
    T ret() {return val;}
    T val;
};

template <typename... ARGS>
std::tuple<ARGS...> readvals(std::tuple<A<ARGS>...> targ)
{
    //???
}

int main(int argc, char **argv)
{
    A<int> ai = A<int>(5);
    A<char> ac = A<char>('c');
    A<double> ad = A<double>(0.5);


    std::tuple<A<int>,A<char>,A<double>> at = std::make_tuple(ai,ac,ad);

    // assuming proper overloading of "<<" for tuples exists
    std::cout << readvals<int,char,double>(at) << std::endl;
    // I expect something like (5, c, 0.5) in the output
    return 0;
}

我发现了关于 SO 的问题,部分解决了这个问题(元组解包、迭代元组元素等),但在我看来,与将所有这些解决方案放在一起相比,应该有一个更简单的解决方案。

What is the best way to write the readvals function in the following code without using Boost? Basically, it should get a tuple, call a specific function of it's elemets and return the generated results as a tuple again.

Is there any C++0X-based Functor definition library for tuples?

template <class T>
struct A
{
    A(T _val):val(_val){}
    T ret() {return val;}
    T val;
};

template <typename... ARGS>
std::tuple<ARGS...> readvals(std::tuple<A<ARGS>...> targ)
{
    //???
}

int main(int argc, char **argv)
{
    A<int> ai = A<int>(5);
    A<char> ac = A<char>('c');
    A<double> ad = A<double>(0.5);


    std::tuple<A<int>,A<char>,A<double>> at = std::make_tuple(ai,ac,ad);

    // assuming proper overloading of "<<" for tuples exists
    std::cout << readvals<int,char,double>(at) << std::endl;
    // I expect something like (5, c, 0.5) in the output
    return 0;
}

I have found questions on SO which deal partly with this problem (tuple unpacking, iterating over tuple elements, etc.), but it seems to me that there should be an easier solution compared to putting together all such solutions.

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

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

发布评论

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

评论(2

简单爱 2024-12-07 08:15:43

如果我理解正确的话,您只想创建一个新元组,其内容是应用于旧元组内容的函数的结果?就像这样:

std::tuple<A,B,C> result =
  std::tuple<A,B,C>(f(std::get<0>(x), f(std::get<1>(x), f(std::get<2>(x));

这是对的吗?为了回答这个问题,我正在窃取 @Luc Danton 的优秀元组索引器。从本质上讲,这种构造允许我们编写:

std::tuple<Args...> result = std::tuple<Args...>(f(std::get<Indices>(x))...);

它是如何工作的:首先,Indices 帮助器:

#include <tuple>

template<int... Indices>
struct indices {
  typedef indices<Indices..., sizeof...(Indices)> next;
};

template<int Size>
struct build_indices {
  typedef typename build_indices<Size - 1>::type::next type;
};

template<>
struct build_indices<0> {
  typedef indices<> type;
};

template<typename Tuple>
typename build_indices<std::tuple_size<typename std::decay<Tuple>::type>::value>::type
make_indices()
{
  return {};
}

现在对于应用程序:我们只需创建一个简单的固定函数f > 使其输入加倍。

template <typename T> T f(const T & t) { return 2*t; }

让我们将其应用于元组。这是一个硬连线函数,但您可以轻松地在 f 上对其进行模板化:

template <typename Tuple, int ...Indices>
Tuple apply_f_impl(const Tuple & x, indices<Indices...>)
{
  return Tuple(f(std::get<Indices>(x))...);
}

template <typename Tuple>
Tuple apply_f(const Tuple & x)
{
  return apply_f_impl(x, make_indices<Tuple>());
}

最后是测试用例:

#include <iostream>
#include "prettyprint.hpp"

int main()
{
  std::tuple<int, double, char> x(5, 1.5, 'a');
  auto y = apply_f(x);

  std::cout << "Before: " << x << ", after: " << y << std::endl;
}

所有这一切都应归功于 Luc,他提出了自索引元组索引器。

If I understand correctly, you just want to make a new tuple whose contents are the results of a function applied to the contents of an old tuple? Like so:

std::tuple<A,B,C> result =
  std::tuple<A,B,C>(f(std::get<0>(x), f(std::get<1>(x), f(std::get<2>(x));

Is this right? To answer that, I am stealing @Luc Danton's excellent tuple indexer. At the very heart, this construction allows us to write:

std::tuple<Args...> result = std::tuple<Args...>(f(std::get<Indices>(x))...);

Here's how it works: First, the Indices helper:

#include <tuple>

template<int... Indices>
struct indices {
  typedef indices<Indices..., sizeof...(Indices)> next;
};

template<int Size>
struct build_indices {
  typedef typename build_indices<Size - 1>::type::next type;
};

template<>
struct build_indices<0> {
  typedef indices<> type;
};

template<typename Tuple>
typename build_indices<std::tuple_size<typename std::decay<Tuple>::type>::value>::type
make_indices()
{
  return {};
}

Now for the application: We just make a simple, fixed function f that doubles its input.

template <typename T> T f(const T & t) { return 2*t; }

Let's apply that to a tuple. Here's a hardwired function, but you can easily template that on f:

template <typename Tuple, int ...Indices>
Tuple apply_f_impl(const Tuple & x, indices<Indices...>)
{
  return Tuple(f(std::get<Indices>(x))...);
}

template <typename Tuple>
Tuple apply_f(const Tuple & x)
{
  return apply_f_impl(x, make_indices<Tuple>());
}

Finally, the test case:

#include <iostream>
#include "prettyprint.hpp"

int main()
{
  std::tuple<int, double, char> x(5, 1.5, 'a');
  auto y = apply_f(x);

  std::cout << "Before: " << x << ", after: " << y << std::endl;
}

All credits for this should go to Luc, who came up with the self-indexing tuple indexer.

单身狗的梦 2024-12-07 08:15:43

这是您要找的吗?我希望您至少可以使用它作为起点。处理元组和可变参数模板的方法比罗马的方法还要多......

#include <iostream>
#include <tuple>

template<class T>
struct A
{
    T t;
    A( const T& t_ ) : t(t_) { }
};

template<class T>
T func( const A<T>&  t)
{
    std::cout << __PRETTY_FUNCTION__ << " " << t.t << "\n";
    return t.t;
}

template<size_t N,class R,  class T>
struct genh
{
    static void gen( R& ret, const T& t )
    {
        std::cout << __PRETTY_FUNCTION__ << "\n";
        genh<N-1,R,T>::gen(ret,t);
        std::get<N>(ret) = func(std::get<N>(t));
    }
};

template<class R, class T>
struct genh<0,R,T>
{
    static void gen( R& ret, const T& t )
    {
        std::cout << __PRETTY_FUNCTION__ << "\n";
        std::get<0>(ret) = func(std::get<0>(t));
    }
};

template<class... T>
std::tuple<T...> readvals( const std::tuple<A<T>...>& targ )
{
    std::tuple<T...> ret;
    genh<sizeof...(T)-1,std::tuple<T...>,std::tuple<A<T>...>>::gen(ret,targ);
    return ret;
}

int main(int argc, const char *argv[])
{
    A<int> ai = A<int>(5);
    A<char> ac = A<char>('c');
    A<double> ad = A<double>(0.5);


    std::tuple<A<int>,A<char>,A<double>> at = std::make_tuple(ai,ac,ad);

    readvals(at);
    return 0;
}

Is this what you are looking for? I hope you can at least use it as a starting point. There are just more ways to approach working with tuples and variadic templates than there are ways that lead to rome...

#include <iostream>
#include <tuple>

template<class T>
struct A
{
    T t;
    A( const T& t_ ) : t(t_) { }
};

template<class T>
T func( const A<T>&  t)
{
    std::cout << __PRETTY_FUNCTION__ << " " << t.t << "\n";
    return t.t;
}

template<size_t N,class R,  class T>
struct genh
{
    static void gen( R& ret, const T& t )
    {
        std::cout << __PRETTY_FUNCTION__ << "\n";
        genh<N-1,R,T>::gen(ret,t);
        std::get<N>(ret) = func(std::get<N>(t));
    }
};

template<class R, class T>
struct genh<0,R,T>
{
    static void gen( R& ret, const T& t )
    {
        std::cout << __PRETTY_FUNCTION__ << "\n";
        std::get<0>(ret) = func(std::get<0>(t));
    }
};

template<class... T>
std::tuple<T...> readvals( const std::tuple<A<T>...>& targ )
{
    std::tuple<T...> ret;
    genh<sizeof...(T)-1,std::tuple<T...>,std::tuple<A<T>...>>::gen(ret,targ);
    return ret;
}

int main(int argc, const char *argv[])
{
    A<int> ai = A<int>(5);
    A<char> ac = A<char>('c');
    A<double> ad = A<double>(0.5);


    std::tuple<A<int>,A<char>,A<double>> at = std::make_tuple(ai,ac,ad);

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