繁体   English   中英

宏引用扩展结果

[英]Macro quote the expanded result

我有一个这样的宏

#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);\

}

当我用

JSON_SPIRIT_SAFE_LOOKUP(str);

我懂了

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

__type不扩展的地方。 有人知道如何扩展吗?

例如,您应该使用两阶段宏进行串联。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM