2

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に関する質問を見つけましたが、そのようなすべてのソリューションをまとめるよりも簡単なソリューションがあるはずです。

4

2 に答える 2

8

私が正しく理解していれば、古いタプルのコンテンツに適用された関数の結果であるコンテンツを持つ新しいタプルを作成したいだけですか?そのようです:

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));

これは正しいですか?これに答えるために、@LucDantonの優れたタプルインデクサーを盗んでいます。本質的に、この構造により、次のように書くことができます。

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次に、アプリケーションについて説明します。入力を2倍にする単純な固定関数を作成するだけです。

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に送られる必要があります。

于 2011-08-16T14:19:38.810 に答える
0

これはあなたが探しているものですか?少なくとも出発点としてご利用いただければ幸いです。タプルと可変個引数テンプレートを使用して作業する方法は、ローマにつながる方法よりもはるかに多くあります...

#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;
}
于 2011-08-16T13:58:20.780 に答える