为什么MSVC会在编辑宏时发脾气,而G ++是关于zen的呢?

Gre*_*ory 3 c c++ visual-c++

在MSVC 9.0下,这失败了.在g ++下编译.如果我们取出宏,则非宏版本76-79编译.有任何想法吗?

03: #include <iostream>
04: #include <sstream>
67: #define MAKESTRING(msg, v) \
68:        do { \
69:          std::ostringstream s; \
70:          s << msg; \ 
71:          v = s.str(); \
72:        } while(false)
73:        
74:        int main(void)
75:        { 
76:          std::ostringstream oss;
77:          std::string str;
78:          oss << "foo" << "bar";
79:          str = oss.str();
80:        
81:          MAKESTRING("foo" << "bar", str);
82:         }

testenv.cpp(71) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
testenv.cpp(71) : error C2065: 's' : undeclared identifier
testenv.cpp(71) : error C2228: left of '.str' must have class/struct/union
1>        type is ''unknown-type''
testenv.cpp(72) : error C2059: syntax error : '}'
testenv.cpp(72) : error C2143: syntax error : missing ';' before '}'
testenv.cpp(72) : error C2059: syntax error : '}'
testenv.cpp(75) : error C2143: syntax error : missing ';' before '{'
testenv.cpp(75) : error C2447: '{' : missing function header (old-style formal list?)
testenv.cpp(81) : error C2017: illegal escape sequence
testenv.cpp(126) : fatal error C1004: unexpected end-of-file found
Run Code Online (Sandbox Code Playgroud)

Gre*_*ill 17

我会确保在用于分隔宏的行的反斜杠之后没有任何尾随空格.由于编译器报告是行号的宏定义,这意味着预处理器还没有完全做到你期望的.

还可以尝试使用MSVC /E编译选项运行它,以查看预处理源的外观.

实际上,即使在您粘贴到问题中的源代码中,第70行也有一个尾随空格.:)