2008-12-10 22 views
0

我暂时使用gcc 2.95.2,而不是使用sstream标题,它定义了一个(略有不同且不赞成的)strstream。目前我得到解决此与如何处理来自旧编译器的sstream与strstream不一致问题

#if __GNUC__ < 3  // or whatever version number it changes 
#include <strstream> 
#else 
#include <sstream> 
#endif 

再之类的东西:

#if __GNUC__ < 3 
    strstream str; 
    str << "Hello World"; 
#else 
    stringstream str("Hello World"); 
#endif 

,但它变得真的很烦人。我只想确保当我切换回更新的gcc(或其他编译器)时,我不必重写这些段落。有什么想法吗?

回答

2

创建mystream.h作为

#ifndef mystream 

#if __GNUC__ < 3  // or whatever version number it changes 
#include <strstream> 
#define mystream(x,y) strstream x; x << y; 
#else 
#include <sstream> 
#define mystream(x,y) sstream x(y); 
#endif 

#endif 

然后使用mystream.h头和mystream类型代替。

如果你真的想让它看起来像现代流,你可以手动创建一个新类(借助更新的std C++库源代码或手动创建一个使用strstream作为底层工作方式的代理类) 。

+0

我想过这个,但我真的希望代码基本上看起来像现代的stringstream,并且有一些小的语法差异。 – 2008-12-10 18:43:59