Why does MSVC throw a tantrum while compiling a macro, and g ++ is all about zen?

In MSVC 9.0 this fails. Under g ++, this compiles. If we choose a macro, then compiling non-macros is 76-79. Any ideas?

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
+3
source share
1 answer

I would make sure you don't have trailing spaces after the backslashes that you use to separate the lines of your macro. Since the compiler reports line numbers that are within your macro definition, this means that the preprocessor did not quite do what you expect.

Also try doing this with the MSVC compilation option /Eto see what the processed source code looks like.

, , , 70 .:)

+17

Source: https://habr.com/ru/post/1719798/


All Articles