//Purpose: // Prototype method for creating container // which has contructor taking "named component arguments". // IOW, a ctor taking a sequence of: // // fusion::pair // // where Index is mpl::advance >::type // for some 0<=i #include #include #include template struct ti { int v; ti(int a=-1) : v(a) {} }; template < int I > std::ostream& operator<< ( std::ostream& sout , ticonst& x ) { sout<<"ti<"<("< struct ctor_default_args : TailArgs { typedef ctor_default_args type ; using TailArgs:: at_key ; typename KeyValue::second my_value ; typename KeyValue::second const& at_key(typename KeyValue::first)const { return my_value; } typename KeyValue::second& at_key(typename KeyValue::first) { return my_value; } }; using namespace boost; template < typename... TypePairs > struct map_default : mpl::fold_assoc_pack < mpl::assoc_right , ctor_default_args , end_args , TypePairs... >::type { void put ( void ) { } template < typename HeadValue , typename... TailValues > void put ( HeadValue const& head_arg , TailValues const&... tail_args ) { typedef typename HeadValue::first_type key_type; typedef typename HeadValue::second_type value_type; value_type& v=this->at_key(key_type()); v=head_arg.second; put(tail_args...); } template < typename... ValuePairs > map_default ( ValuePairs const&... value_pairs ) { put(value_pairs...); } }; int main(void) { typedef map_default < mpl::pair,ti<1> > , mpl::pair,ti<2> > , mpl::pair,ti<3> > > map_def_ti1_3_t; map_def_ti1_3_t map_def_ti1_3_def; std::cout<<"-----------------------\n"; std::cout<<"default values for map:\n"; std::cout<<"-----------------------\n"; std::cout<<"at_key1="<())<<"\n"; std::cout<<"at_key2="<())<<"\n"; std::cout<<"at_key3="<())<<"\n"; map_def_ti1_3_t map_def_ti1_3_val2_1 ( fusion::pair,ti<2> >(2) , fusion::pair,ti<1> >(1) ) ; std::cout<<"-----------------------------\n"; std::cout<<"named/default values for map:\n"; std::cout<<"-----------------------------\n"; std::cout<<"at_key1="<())<<"\n"; std::cout<<"at_key2="<())<<"\n"; std::cout<<"at_key3="<())<<"\n"; return 0; }