leon
leon

Reputation: 5091

Macro quote the expanded result

I have a macro like this

#define REQUIRE(condition,message) \
  if (!(condition)) { \
    std::ostringstream _msg_stream; \
    _msg_stream << __FILE__ <<" at line: " << __LINE__ << " becasue " << message << std::endl; \
    throw Error(_msg_stream.str());\
  } else

#define QUOTEME_(x) #x
#define QUOTEME(x) QUOTEME_(x)

#define JSON_SPIRIT_SAFE_LOOKUP(__type) \
  if(iter != obj.end()){\
    if(iter->second.type() == json_spirit::##__type##_type) { \
  return iter->second.get_##__type##(); \
}else{\
  std::cerr << "JsonDump: "<<obj << std::endl;\
  REQUIRE(false, " The value type is not of type " << QUOTEME(__type) <<\
                   " with key: " << key << std::endl);\
}\
  }else{\
std::cerr << "JsonDump: "<<obj << std::endl;\
REQUIRE(false, " Cannot look up by the key: " << key<<std::endl);\

}

and when I invoke it with

JSON_SPIRIT_SAFE_LOOKUP(str);

I get

ID: 6172: ..\phx\jsonutililty.cpp at line: 107 becasue  The value type is not of type __type with key: blah

where __type is not expanded. Anyone know how to expand it?

Upvotes: 2

Views: 298

Answers (1)

Puppy
Puppy

Reputation: 146900

You should use a two-stage macro for concatenation, for example.

#define CONCAT_(x, y) x ## y
#define CONCAT(x, y) CONCAT_(x, y)

if (iter->second.type() == CONCAT(CONCAT(json_spirit::, __type), _type)

Upvotes: 1

Related Questions