是否有建议将STD :: bin添加到C 标准中
Has there been a proposal to add std::bin to the c++ standard?
c 14通过输入0b
前缀的值来增加使用二进制文字的能力:
int v = 0b1111; // 15 in decimal
但是,std::hex
或std::oct
等流的std::bin
操纵器没有。所以我需要使用,例如std::bitset
用于打印目的:
std::cout << std::bitset<4>(v) << "n";
是否已提出或考虑过?如果是这样,这个想法的状态是什么?
据我所知,没有提交格式标志的提议来添加二进制格式和/或操纵器std::bin
。您可以在http://www.open-std.org/jtc1/sc22/wg21/wg21/docs/papers/上查看建议。我很确定添加二进制文字的建议没有添加此功能(快速搜索揭示了N3472,但我不确定这是否是本文的最新版本)。
从技术角度来看,添加可能并不容易!通常,各种标志都仅以一个单词的形式存储在流类中,并且有各种原因可以用尽所有位。现有的三个设置(std::ios_base::oct
,std::ios_base::dec
,std::ios_base::hex
)只能在2位中很好地存储。当然,这三个值将使一个值打开,除了该值通常用于默认设置,即在读取时不固定基础。结果,可能有必要更改流类的布局或使处理效率降低(例如,通过以某种方式使用iword()
来存储二进制格式化的其他可能性)。我尚未进行分析,是否有任何实现存在实际问题(我知道我的实现都没有,但是如果我没错的话,我确实使用了单词中的所有位)。
如果要支持二进制格式,则相对容易通过自定义std::num_put<char>
刻面添加。以下是一个简单的示例。它不处理一些可能需要的格式选项,例如填充或数字分离器:
#include <iostream>
#include <limits>
#include <locale>
class binary_num_put
: public std::num_put<char> {
template <typename T>
iter_type common_put(iter_type out, std::ios_base& str, char_type fill,
T original, unsigned long long v) const {
if (str.flags() & std::ios_base::basefield) {
return this->std::num_put<char>::do_put(out, str, fill, original);
}
if (str.flags() & std::ios_base::showbase) {
*out++ = '0';
*out++ = str.flags() & std::ios_base::uppercase? 'B': 'b';
}
unsigned long long mask(1ull << (std::numeric_limits<unsigned long long>::digits - 1));
while (mask && !(mask & v)) {
mask >>= 1;
}
if (mask) {
for (; mask; mask >>= 1) {
*out++ = v & mask? '1': '0';
}
}
else {
*out++ = '0';
}
return out;
}
iter_type do_put(iter_type out, std::ios_base& str, char_type fill, long v) const {
return common_put(out, str, fill, v, static_cast<unsigned long>(v));
}
iter_type do_put(iter_type out, std::ios_base& str, char_type fill, long long v) const {
return common_put(out, str, fill, v, static_cast<unsigned long long>(v));
}
iter_type do_put(iter_type out, std::ios_base& str, char_type fill, unsigned long v) const {
return common_put(out, str, fill, v, v);
}
iter_type do_put(iter_type out, std::ios_base& str, char_type fill, unsigned long long v) const {
return common_put(out, str, fill, v, v);
}
};
std::ostream& bin(std::ostream& out) {
auto const& facet = std::use_facet<std::num_get<char>>(out.getloc());
if (!dynamic_cast<binary_num_put const*>(&facet)) {
std::locale loc(std::locale(), new binary_num_put);
out.imbue(loc);
}
out.setf(std::ios_base::fmtflags(), std::ios_base::basefield);
return out;
}
int main()
{
std::cout << std::showbase << bin << 12345 << " "
<< std::dec << 12345 << "n";
}
相关文章:
- 如何将标准容器作为字段添加到 OMNet++ 消息中?
- 如何添加到标准::变体?
- 如何在不使用标准算法的情况下在排序向量中添加 c 元素?
- 哪个C 标准包括要添加到对象文件中的文件强制代码 /数据
- 使用连接器 c++ 访问 MYSQL 数据库(如何将路径添加到我的标准搜索目录)
- 我无法将项目添加到 ( 标准::矢量 < 标准::矢量 <标准::字符串> > )
- 标准::时间 将天数添加到当前日期
- 是否有建议将STD :: bin添加到C 标准中
- Windows Batch - 如何在将标准输出重定向到文件时添加时间戳
- 添加标准::p空气和 + 运算符
- visual 是 -Dfoobar 为所有编译器添加定义的标准C++
- 值初始化是 C++98 标准的一部分吗?如果不是,为什么在 C++03 标准中添加它
- 使用FindLibrary.cmake向cmake添加非标准库
- 添加带有自定义标准错误的 fprintf() 语句后,标准输出挂起
- 如何添加标准对象和用户定义的对象
- 无法将整数添加到标准::复杂<double>
- c++标准是否允许添加两个整数(基本类型int)来抛出c++异常?
- 向size_t添加const会导致编译失败,这是标准行为吗
- 阻止Apple clang在标准include路径中添加Xcode路径
- 如何在c++中添加非标准库