2016-09-22 10 views
6

기본 클래스가 멤버 인 것처럼 기본 클래스를 융합 할 수 있습니까?기본 클래스를 융합 할 수 있습니까?

먼저이 문서의 예입니다, 나란히 새로운 케이스 : 나는 ???과 라인에 넣어해야합니까

#include <boost/fusion/adapted/struct/adapt_struct.hpp> 
#include <boost/fusion/include/adapt_struct.hpp> 

struct employee{ 
    std::string name; 
    int age; 
}; 

BOOST_FUSION_ADAPT_STRUCT(
    employee, 
    (std::string, name) 
    (int, age)) 

struct employee2 : std::string{ 
    int age; 
}; 

BOOST_FUSION_ADAPT_STRUCT(
    employee2, 
    (std::string, name) ??? 
    (int, age)) 


int main(){} 

.

현재 내가 찾은 유일한 해결책은이 작업을 수행하는 것입니다. 그러나 1) 모든 구성원을 게터 및 설정 기능으로 만들어야합니다. 2) 과도한 것 같습니다.

#include <boost/fusion/adapted/adt/adapt_adt.hpp> 
struct employee2 : std::string{ 
    int age; 
    void set_base(std::string const& age_){std::string::operator=(age_);} 
    std::string const& get_base() const{return static_cast<std::string const&>(*this);} 
    void set_age(int const& age_){age = age_;} 
    int const& get_age() const{return age;} 
}; 

BOOST_FUSION_ADAPT_ADT(
    employee2, 
    (std::string, std::string, obj.get_base(), obj.set_base(val)) 
    (int, int, obj.get_age(), obj.set_age(val)) 
) 

답변

0

글쎄, 그것은 하나 BOOST_FUSION_ADAPT_ADT에 유효한 표현의 모든 종류를 넣을 수 (실험에 의해) 보인다. 이것이 최적 인 지 잘 모르겠다. (예를 들어, 융합이 요소에 접근 할 때 사본을 만들면), 다른 답변도 환영 받는다.

#include <boost/fusion/adapted/adt/adapt_adt.hpp> 
BOOST_FUSION_ADAPT_ADT(
    employee2, 
    (static_cast<std::string const&>(obj), obj.std::string::operator=(val)) 
    (obj.age, obj.age = val) 
) 

int main(){ 

    employee2 e2; 
    boost::fusion::at_c<0>(e2) = "Pepe"; 
    boost::fusion::at_c<1>(e2) = 37; 

    cout << e2 << " " << e2.age <<'\n'; 

} 

이 일부 사본을 방지하고 (예를 들어, boost::fusion::copy) 많은 경우에 작동하는 것 같다 그러나 나는 확실하지 않다 수있는 이유 :

BOOST_FUSION_ADAPT_ADT(
    employee2, 
    (std::string, std::string const&, obj, obj.std::string::operator=(val)) 
    (int, int const&, obj.age, obj.age = val) 
)