Mae*_*anz 14 c++ methods function precompile c++11
我还没有使用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
Run Code Online (Sandbox Code Playgroud)
这显然不起作用.这样的事情是可能的,如果是的话,怎么样?
Bia*_*sta 14
这是namespace存在的主要目的之一.
我的建议是将您的个人功能包含在适当的命名空间中,例如:
namespace myns {
std::string to_string(...) {
// ...
}
// etc...
}
Run Code Online (Sandbox Code Playgroud)
这是避免未来冲突问题的根本.
之后,当您打算使用该功能时,您可以通过MACRO替换简单地选择适当的功能.
就像是:
#if (__cplusplus >= 201103L)
#define my_tostring(X) std::to_string(X)
#else
#define my_tostring(X) myns::to_string(X)
#endif
Run Code Online (Sandbox Code Playgroud)
注意__cplusplus是一个预定义的宏,其中包含有关标准版本的编译信息.
编辑:
不那么"暴力",它将根据标准版本为该特定功能选择适当的命名空间:
#if (__cplusplus >= 201103L)
using std::to_string;
#else
using myns::to_string;
#endif
// ... somewhere
to_string(/*...*/); // it should use the proper namespace
Run Code Online (Sandbox Code Playgroud)
您无法测试它们是否被定义为此类,但您可以检查语言版本:
#if __cplusplus < 201103L
Run Code Online (Sandbox Code Playgroud)
(有预定义的编译器宏的有用集合在这里.)
| 归档时间: |
|
| 查看次数: |
2376 次 |
| 最近记录: |