为什么std :: setbase(2)不切换到二进制输出? [英] Why doesn't std::setbase(2) switch to binary output?

查看:285
本文介绍了为什么std :: setbase(2)不切换到二进制输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std::setbase 上的cppreference页面显示:

除8、10或16以外的基数的值会将基数字段重置为零,这对应于十进制输出和与前缀相关的输入.

Values of base other than 8, 10, or 16 reset basefield to zero, which corresponds to decimal output and prefix-dependent input.

怎么来?

是否只有特定原因支持这些基础?支持至少16个(实际上是36:0-9,然后是a-z)任何东西,而不必做出任何困难的选择似乎是微不足道的.具体来说,2是一个受欢迎的基础,我认为应该对std::setbase(2)(和相应的std::binary)感兴趣.

Is there a particular reason why only these bases are supported? It seems trivial to support at least anything up to 16 (actually, up to 36: 0-9 and then a-z) without having to make any sort of difficult choices. Specifically, 2 is a popular base, I would think there should be interest in std::setbase(2) (and a corresponding std::binary).

我显然可以打印自己的位,但是对我的ostream来说,这样做很不错.

I can obviously print my own bits but it would have been nice for my ostream to do it.

推荐答案

唯一可以肯定的答案是因为标准如此规定".

The only sure answer is "because the standard says so".

话虽这么说,该标准大多数形式化了标准前的iostream实施,其中许多可能只是为了与printf达到功能奇偶性而设计的,printf仅支持十进制,八进制和十六进制(通过即席而不是-真正通用的语法).

That being said, the standard mostly formalized the pre-standard iostream implementations, many of which probably were designed just to reach feature parity with printf, which supports only decimal, octal and hexadecimal (through ad-hoc, not-really-general syntax).

而且,鉴于现在的基础设置最终会出现在位域中,而不是在单独的当前基础"域中,因此修补iostream API以支持许多"基础并不是一件容易的事.

Also, as it is now it wouldn't be trivial to patch the iostream API to support "many" bases, given that the base setting ends up into a bitfield, not in a separate "current base" field.

现在,该标准要求fmtflags属于某种位掩码类型"-甚至可能是std::bitset,因此您可能会找到一种以某种方式铲除所有这些新的基础"字段的方法-确实如此对于几乎没有人真正关心的功能,值得付出努力(加上冒犯fmtflags是整数类型的代码的风险)吗?

Now, the standard requires fmtflags to be of some "bitmask type" - which may even be a std::bitset, so you may find a way to shovel all those new "base" fields somehow - but is it really worth the effort (plus the risk of breaking the code that assumes that fmtflags is of an integral type) for a feature that almost nobody really cares about?

因此,总而言之:糟糕的初始设计(实际上就像iostream的其余部分一样),不平凡的修复以及用户对这种功能的真正需求.

So, to sum it up: bad initial design (like the rest of iostream, actually), nontrivial fix and no real user demand for such a feature.

这篇关于为什么std :: setbase(2)不切换到二进制输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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