c++ - C++11 中的 std::basic_string::npos 声明

标签 c++ string c++11

basic_string 类有 npos 声明为静态常量。 为什么自 C++11 起它声明为 static const,为什么不简单为:

class basic_string{ 
  ................................

 enum: size_type { npos = static_cast<size_type>(-1) };
.........................>
};

???

static const 和 enum 哪个好?

最佳答案

有一个很好的理由不这样做,枚举创建了一个新类型,它至少会在解析重载或实例化模板时引起变化。

也就是说,我相信您实际上可以在类定义中声明和定义类静态常量,或者当类是模板时该规则有一些异常(exception)吗?

关于c++ - C++11 中的 std::basic_string::npos 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17548082/

相关文章:

c++ - 优化正在取消我在 clang 6 中的整数溢出检查

c++ - 异步运行的函数的返回类型应该是什么

c++ - 从 unique_ptr<T> 的 void* 转换为 T** 是如何工作的?

c++ - 如果 std::thread 对自身调用 joinable 会发生什么?

c++ - 将 protobuf 消息拆分到多个输出缓冲区

c++ - OS X Yosemite的SciTe-错误: cannot be used within an App Sandbox

string - Mips如何存储用户输入字符串

c++ - 如何使用类的类型作为模板的参数

Android string.xml 颜色

java - 使用堆栈来反转字符串?