我还没有使用C++11,所以我自己编写了to_string(whatever)
函数。如果它们不存在,它们应该被编译。如果我切换到C++11,它们应该被跳过。我的做法如下:
#ifndef to_string
string to_string(int a){
string ret;
stringstream b;
b << a;
b >> ret;
return ret;
}
string to_string(double a){
string ret;
stringstream b;
b << a;
b >> ret;
return ret;
}
#endif
这显然不起作用。类似这样的东西可行吗?如果是,怎么做?
std::to_string
是C++11的东西。 - xinaizto_string
,但是有std::to_string
,它们之间有很大的区别。这意味着,如果您的标准不支持它,就不能使用std::to_string
。现在再想想-假设您已经使用了C++11,那么现在怎么办?如果您使用接受的答案中的宏,您会终身使用它,而不是使用std::to_string
吗?非常非常糟糕的想法。 - xinaiz#ifdef
仅测试预处理器定义的存在,而不是像std::to_string
这样的常规函数。这就是#ifdef
可以测试的全部内容,因为预处理器指令由...预处理器执行。 - Marc van Leeuwen