为什么std :: string :: max_size不是编译时常量? [英] Why isn't std::string::max_size a compile-time constant?

查看:172
本文介绍了为什么std :: string :: max_size不是编译时常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: string 提供了一个 max_size()方法来确定它可以包含的最大元素数。

std::string provides a max_size() method to determine the maximum number of elements it can contain.

但是,为了计算一个字符串的最大长度,程序员必须创建一个(可能为空的)字符串对象。

However, to work out the maximum length of a string in general, the programmer has to create a (possibly empty) string object.

如果这个类不需要程序员的任何信息,为什么 max_size()可用作编译时常量?

If this class doesn't need any information from the programmer, why isn't max_size() available as a compile-time constant ? Is there some kind of runtime information necessary for a string to work out its maximum size ?

推荐答案

有一个原因是 max_size 函数根本不是很有用,委员会认为不值得尝试解决它的麻烦。

One reason is that the max_size function isn't very useful at all, and the committee doesn't think it is worth the trouble to try to fix it. So it is just left the way it is, because it is part of the documented interface.

查看库缺陷报告#197:

See library defect report #197:

http://www.open- std.org/jtc1/sc22/wg21/docs/papers/2012/n3440.html#197


max_size()对于很多事情没有用,现有的措辞对于max_size()可以使用的少数情况是足够清楚的。没有改变现有措辞的尝试是改进。

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.

这篇关于为什么std :: string :: max_size不是编译时常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆