簡體   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