c++ - Using boost preprocessor to call a variadic template iteratively -


assume have variadic template:

template<typename... args> class foo; 

this variadic template generates template recursively until reaches 1 argument foo in last level. want have macro example bar(...) when call this:

bar(float, int, string, vector<int>) // expands macro(foo<float, int, string, vector<int>>) macro(foo<int, string, vector<int>>) macro(foo<string, vector<int>>) macro(foo<vector<int>>) 

which macro(...) macro doing on class. hope able use boost preprocessor reduce code have write.

please suggest me tips helps me write such macro.

i don't know if best approach solving problem want:

#include <boost/preprocessor/repetition/repeat_from_to.hpp> #include <boost/preprocessor/seq.hpp> #include <boost/preprocessor/variadic/to_seq.hpp>  #define call_macro(_,__,seq) macro(foo<boost_pp_seq_enum(seq)>)  #define generate_macro_invocations(seqs) boost_pp_seq_for_each(call_macro,_,seqs)  #define generate_descending_sequences(_,index,data) (boost_pp_seq_rest_n(index,data))  #define bar_impl(seq) generate_macro_invocations(boost_pp_repeat_from_to(0,boost_pp_seq_size(seq),generate_descending_sequences, seq))   #define bar(...) bar_impl(boost_pp_variadic_to_seq(__va_args__))  bar(float, int, string, vector<int>) 
  • you have variadic data: float, int, string, vector<int>.
  • boost_pp_variadic_to_seq transforms to: (float)(int)(string)(vector<int>)
  • boost_pp_repeat_from_to calls macro generate_descending_sequences boost_pp_seq_size(seq) times sequence data , index starts 0.
  • boost_pp_seq_rest_n(index,data) removes index first elements data , returns rest. result put inside couple of parentheses.
  • after invocation of repeat have sequence of sequences:

    ((float)(int)(string)(vector))((int)(string)(vector))((string)(vector))((vector))

  • boost_pp_seq_for_each calls call_macro every element in sequence of sequences.

  • and boost_pp_seq_enum takes sequence , returns elements separated commas.

preprocessed on coliru


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -