2013-09-03 5 views
1

나는 다른 종류의 여러 구성원 데이터를 요소와 융합 벡터를하고 난 단지 특정 데이터 멤버 (들)을 투영하는 새로운 융합 벡터 (들)을 생성하고 싶습니다. 나는이 문제에 대해 이미 조사했지만 진전이 없었다.퓨전 벡터 투사

#include <iostream> 
#include <string> 
#include <boost/fusion/adapted/boost_tuple.hpp> 
#include <boost/fusion/include/fold.hpp> 
#include <boost/fusion/include/sequence.hpp> 
#include <boost/fusion/algorithm.hpp> 
#include <boost/spirit/include/phoenix.hpp> 
#include <boost/fusion/include/make_vector.hpp> 
#include <boost/bind.hpp> 

namespace phx = boost::phoenix; 
namespace fusion = boost::fusion; 
using namespace phx::arg_names; 

struct mprint 
{ 
    template<typename T> 
    void operator()(T& t) const 
    { 
     std::cout << t << std::endl; 
    } 
}; 

struct TStruct 
{ 
    std::string val1_; 
    double  val2_; 
    int   val3_; 
    bool  val4_; 
    TStruct(const std::string &val1, double val2, int val3, bool val4) : 
     val1_(val1), val2_(val2), val3_(val3), val4_(val4) { } 
}; 

template <typename Sequence> 
void viewVal1(const std::string &dummy,const Sequence& args) 
{ 
    fusion::for_each(boost::tie(phx::bind(&TStruct::val1_, arg1)(args)), mprint()); 
} 

template <typename Sequence> 
void viewVal2(const std::string &dummy,const Sequence& args) 
{ 
    //fusion::for_each(boost::tie(phx::bind(&TStruct::val2_, arg1)(args)), mprint()); 
} 

int main(int argc, char* argv[]) 
{ 
    auto mdata = fusion::make_vector(
      TStruct("test1", 2.3, 1, true), 
      TStruct("test2", 3.3, 2, false), 
      TStruct("test3", 4.3, 3, true) 
    ); 

    viewVal1("dummy", mdata); 
    viewVal2("dummy", mdata); 

    return 0; 
} 

답변

2

나는 당신이 사용해야하는 fusion::transform_view이다 (잘 모르겠어요 의미) 생각 :

Running on Coliru을 :

#include <iostream> 
#include <string> 
#include <boost/fusion/adapted/boost_tuple.hpp> 
#include <boost/fusion/include/fold.hpp> 
#include <boost/fusion/include/sequence.hpp> 
#include <boost/fusion/algorithm.hpp> 
#include <boost/spirit/include/phoenix.hpp> 
#include <boost/fusion/include/make_vector.hpp> 
#include <boost/bind.hpp> 

namespace phx = boost::phoenix; 
namespace fusion = boost::fusion; 
using namespace phx::arg_names; 

struct mprint 
{ 
    template<typename T> 
    void operator()(const T& t) const 
    { 
     std::cout << t << std::endl; 
    } 
}; 

struct get_val1 
{ 
    template <typename T> 
    std::string operator()(const T& t) const 
    { 
     return t.val1_; 
    } 
}; 

struct TStruct 
{ 
    std::string val1_; 
    double  val2_; 
    int   val3_; 
    bool  val4_; 
    TStruct(const std::string &val1, double val2, int val3, bool val4) : 
     val1_(val1), val2_(val2), val3_(val3), val4_(val4) { } 
}; 

template <typename Sequence> 
void viewVal1(const std::string &dummy, Sequence& args) 
{ 
    typedef fusion::transform_view<Sequence, get_val1> project; 
    fusion::for_each(project(args,get_val1()), mprint()); 
} 

template <typename Sequence> 
void viewVal2(const std::string &dummy, Sequence& args) 
{ 
    typedef fusion::transform_view<Sequence,decltype(phx::bind(&TStruct::val2_,arg1))> project; 
    fusion::for_each(project(args,phx::bind(&TStruct::val2_, arg1)), mprint()); 
} 

int main(int argc, char* argv[]) 
{ 
    auto mdata = fusion::make_vector(
      TStruct("test1", 2.3, 1, true), 
      TStruct("test2", 3.3, 2, false), 
      TStruct("test3", 4.3, 3, true) 
    ); 

    viewVal1("dummy", mdata); 
    viewVal2("dummy", mdata); 

    return 0; 
} 
+1

안녕하세요! 두 가지 접근 방식을 제공 해주셔서 대단히 감사합니다. 그냥 운동이고 내가 다시 추가 할 필요 :-) 나는이 나를 위해 컴파일 있도록이 작업을 만들 수 없습니다 : '# 만약 __cplusplus> = 201103L #DEFINE BOOST_RESULT_OF_USE_DECLTYPE # endif' –