Is it possible for merging to adapt the base class?

Is it possible to smooth out an adaptive base class as if it were a member?

First up is a sample documentation, side by side with a new case:

#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(){} 

What should I put in a string with ??? .

Currently, the only solution I have found is to do this, but 1) I have to execute all the getter and setter functions 2) it seems to be crowded.

 #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)) ) 
+6
source share
1 answer

Well, he (experimentally) can put all kinds of valid expressions in BOOST_FUSION_ADAPT_ADT . I'm not very sure if this is optimal (for example, if fusion will make copies when accessing elements), so other answers are welcome.

 #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'; } 

This can prevent some copies and seems to work in more cases (e.g. boost::fusion::copy ), but I'm not sure why:

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

All Articles