是否有建议将STD :: bin添加到C 标准中

Has there been a proposal to add std::bin to the c++ standard?

本文关键字:添加 标准 bin 是否 STD      更新时间:2023-10-16

c 14通过输入0b前缀的值来增加使用二进制文字的能力:

int v = 0b1111; // 15 in decimal

但是,std::hexstd::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::octstd::ios_base::decstd::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";
}