内联ostringstream宏重新加载 [英] Inline ostringstream macro reloaded
问题描述
参考 http://stackoverflow.com/questions/303562/c-format-macro -inline-ostringstream
这个问题是一个宏,允许对象的内联串联创建一个字符串iostream风格。
The question there was for a macro that allows inline concatenation of objects to create a string, iostream-style.
答案是:
#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
( std::ostringstream().seekp( 0, std::ios_base::cur ) << x ) \
).str()
用法(例如):
throw std::runtime_error(
SSTR( "FooBar error: Value " << x << " exceeds " << y )
);
这很漂亮 - 与GCC。它编译并运行在Visual C ++ 2005下。但是对于后者,所有使用宏的结果是空字符串,我很茫然为什么,如何解决它...?
That works beautifully - with GCC. It compiles and runs under Visual C++ 2005, too. But with the latter, all uses of the macro result in empty strings, and I am quite dumbfounded as to why, and how to fix it...?
推荐答案
很遗憾,我没有访问MSVC编译器来测试。
Unfortunately I don't have access to a MSVC compiler to test against.
在我以前的微软工具经验,将语言定义和标准视为一个粗略的指南。 (我已经失去了大量的时间项目只发现微软打破传统与基本的C99)。
In my past experiences with microsoft's tools, it seems like microsoft treats language definitions and standards as little more than a rough guide. (I've lost lots of time on projects only to discover microsoft broke tradition with something as basic as C99.)
鉴于这令人遗憾的情况,我建议你实验一个系列琐碎的程序。例如:
Given this regrettably situation, I suggest you experiment with a series of trivial programs. Things like:
std::ostringstream() o;
o.seekp( 0, std::ios_base::cur ) << "foo";
cout << "Test1: " << o << endl;
或者:
std::ostringstream() o;
cout << "Test2: " << typeid(o).name() << endl;
cout << "Test3: " << typeid(o.seekp( 0, std::ios_base::cur )).name() << endl;
尝试看看什么时候停止工作。然后从那里解决问题。
Try to see at what point things stop working. Then work around the problem from there.
这篇关于内联ostringstream宏重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!