2

Why can't i call BOOST_PP_SEQ_FOR_EACH from inside a macro like this:

#define MAP_KEY_TYPES (int)(double)(std::string)
#define MAP_VAL_TYPES (int)(double)(std::string)(std::vector<int>)

#define DECLARE_MAP_VARIANTS(r, K, V) \
    void toJson(        Json::Value &j, const std::map< K, V >& v);\
    void fromJson(const Json::Value &j,       std::map< K, V >& v);

#define DECLARE_MAP_VARIANTS_KEY(r, data, K) \
    BOOST_PP_SEQ_FOR_EACH(DECLARE_MAP_VARIANTS, K, MAP_VAL_TYPES)

    BOOST_PP_SEQ_FOR_EACH(DECLARE_MAP_VARIANTS_KEY, _, MAP_KEY_TYPES)

When I run the preprocesson on this code i get the following:

BOOST_PP_SEQ_FOR_EACH(DECLARE_MAP_VARIANTS, int, (int)(double)(std::string)(std::vector<int>))
BOOST_PP_SEQ_FOR_EACH(DECLARE_MAP_VARIANTS, double, (int)(double)(std::string)(std::vector<int>))
BOOST_PP_SEQ_FOR_EACH(DECLARE_MAP_VARIANTS, std::string, (int)(double)(std::string)(std::vector<int>))

What I would like to see is:

void toJson(        Json::Value &j, const std::map< int,         int >& v);
void fromJson(const Json::Value &j,       std::map< int,         int >& v);
void toJson(        Json::Value &j, const std::map< double,      int >& v);
void fromJson(const Json::Value &j,       std::map< double,      int >& v);
void toJson(        Json::Value &j, const std::map< std::string, int >& v);
void fromJson(const Json::Value &j,       std::map< std::string, int >& v);

void toJson(        Json::Value &j, const std::map< int,         double >& v);
void fromJson(const Json::Value &j,       std::map< int,         double >& v);
void toJson(        Json::Value &j, const std::map< double,      double >& v);
void fromJson(const Json::Value &j,       std::map< double,      double >& v);
void toJson(        Json::Value &j, const std::map< std::string, double >& v);
void fromJson(const Json::Value &j,       std::map< std::string, double >& v);

void toJson(        Json::Value &j, const std::map< int,         std::string >& v);
void fromJson(const Json::Value &j,       std::map< int,         std::string >& v);
void toJson(        Json::Value &j, const std::map< double,      std::string >& v);
void fromJson(const Json::Value &j,       std::map< double,      std::string >& v);
void toJson(        Json::Value &j, const std::map< std::string, std::string >& v);
void fromJson(const Json::Value &j,       std::map< std::string, std::string >& v);

void toJson(        Json::Value &j, const std::map< int,         std::vector<std::string> >& v);
void fromJson(const Json::Value &j,       std::map< int,         std::vector<std::string> >& v);
void toJson(        Json::Value &j, const std::map< double,      std::vector<std::string> >& v);
void fromJson(const Json::Value &j,       std::map< double,      std::vector<std::string> >& v);
void toJson(        Json::Value &j, const std::map< std::string, std::vector<std::string> >& v);
void fromJson(const Json::Value &j,       std::map< std::string, std::vector<std::string> >& v);

How to achieve this?

1
  • 2
    Macro replacement is non-recursive, which is why this doesn't work. If you need multiple levels of nesting, you can consider using BOOST_PP_SEQ_FOR_EACH_I for the second level (disregarding the counter). Commented Dec 31, 2011 at 7:01

1 Answer 1

4

Unfortunately, I suppose this cannot be done only with BOOST_PP_SEQ_FOR_EACH. If BOOST_PP_SEQ_FOR_EACH_PRODUCT is allowed instead, probably the following macro will meet the purpose:

#define DECLARE_MAP_VARIANTS_(r, KV) \
    DECLARE_MAP_VARIANTS(r, BOOST_PP_SEQ_ELEM(0, KV), BOOST_PP_SEQ_ELEM(1, KV))

BOOST_PP_SEQ_FOR_EACH_PRODUCT(
    DECLARE_MAP_VARIANTS_, (MAP_KEY_TYPES)(MAP_VAL_TYPES) )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.