std::string
提供了一个 max_size()
方法来确定它可以包含的最大元素数。
然而,一般来说,要计算出字符串的最大长度,程序员必须创建一个(可能为空的)字符串对象。
如果这个类不需要程序员提供任何信息,为什么 max_size()
不能作为编译时常量使用?字符串计算其最大大小是否需要某种运行时信息?
最佳答案
一个原因是 max_size
函数根本不是很有用,委员会认为尝试修复它不值得。所以它就保持原样,因为它是文档化界面的一部分。
参见库缺陷报告 #197:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3440.html#197
max_size() isn't useful for very many things, and the existing wording is sufficiently clear for the few cases that max_size() can be used for. None of the attempts to change the existing wording were an improvement.
关于c++ - 为什么 std::string::max_size 不是编译时常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13137766/