如果我想将 std::string
类型重命名为更简单、看起来更自然的 string
,我应该使用这两种方法中的哪一种(基于性能和通常是什么标准)
我是否应该将其重命名为预处理指令
#define string std::string
或者用类型定义来做
typedef std::string string;
什么是最高效的方法?还有什么更为社区所熟悉和认可?
最佳答案
只是
using std::string;
如果你想要一个稍微不同的名字,例如
using String = std::string;
避免使用宏,它们不考虑作用域并且通常是 Evil™。
例如提议的宏
#define string std::string
... 如果您包含任何标准库 header ,则会产生正式的未定义行为,因为它定义了标准库使用的名称。
C++11 §17.6.4.2.2/1 [宏名称]:” A translation unit that includes a standard library header shall not
#define
or#undef
names declared in any standard library header.
关于c++ - 什么是最好的 "type renaming"方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37578352/