为什么 std::bitset 只支持整数数据类型?为什么不支持浮动?

Why does std::bitset only support integral data types? Why is float not supported?

尝试生成浮点数的位模式如下:

std::cout << std::bitset<32>(32.5) << std::endl;

编译器生成此警告:

warning: implicit conversion from 'double' to 'unsigned long long' changes value
  from 32.5 to 32 [-Wliteral-conversion]
 std::cout << std::bitset<32>(32.5) << std::endl;

忽略警告时的输出 :) :

00000000000000000000000000100000

为什么 bitset 无法检测浮点数并正确输出位序列,而当转换为 char* 并且行走内存确实显示正确的序列时? 这行得通,但是机器依赖于字节顺序并且大部分不可读:

template <typename T>
  void printMemory(const T& data) {
    const char* begin = reinterpret_cast<const char*>(&data);
    const char* end = begin + sizeof(data);
    while(begin != end)
      std::cout << std::bitset<CHAR_BIT>(*begin++) << " ";
    std::cout << std::endl;
}

输出:

00000000 00000000 00000010 01000010 

有理由不支持浮动吗?花车有替代品吗?

如果您提供一个浮点数,您希望在您的 bitset 中出现什么?大概是大端格式的 IEEE-7545 binary32 浮点数的某种表示形式?那些不以与此类似的方式代表 float 的平台呢?实施是否应该向后弯曲以(可能有损)将提供的浮点数转换为您想要的?

之所以没有,是因为没有标准定义的浮点数格式。它们甚至不必是 32 位。他们只是通常在大多数平台上。

C++ 和 C 将 运行 在非常小的 and/or 奇数平台上。标准不能指望什么是'usually the case'。有 were/are C/C++ 8/16 位 6502 系统的编译器,很抱歉原生浮点格式的借口是(我认为)一个使用 packed BCD encoding.[= 的 6 字节实体14=]

这与 signed 整数不受支持的原因相同。二进制补码不是通用的,只是几乎通用。 :-)

关于浮点格式未标准化、字节顺序等的所有常见警告

这里是 可能 工作的代码,至少在 x86 硬件上是这样。

#include <bitset>
#include <iostream>
#include <type_traits>
#include <cstring>

constexpr std::uint32_t float_to_bits(float in)
{
    std::uint32_t result = 0;
    static_assert(sizeof(float) == sizeof(result), "float is not 32 bits");
    constexpr auto size = sizeof(float);
    std::uint8_t buffer[size] = {};
    // note - memcpy through a byte buffer to satisfy the
    // strict aliasing rule.
    // note that this has no detrimental effect on performance
    // since memcpy is 'magic'
    std::memcpy(buffer, std::addressof(in), size);
    std::memcpy(std::addressof(result), buffer, size);
    return result;
}

constexpr std::uint64_t float_to_bits(double in)
{
    std::uint64_t result = 0;
    static_assert(sizeof(double) == sizeof(result), "double is not 64 bits");
    constexpr auto size = sizeof(double);
    std::uint8_t buffer[size] = {};
    std::memcpy(buffer, std::addressof(in), size);
    std::memcpy(std::addressof(result), buffer, size);
    return result;
}


int main()
{
    std::cout << std::bitset<32>(float_to_bits(float(32.5))) << std::endl;
    std::cout << std::bitset<64>(float_to_bits(32.5)) << std::endl;
}

示例输出:

01000010000000100000000000000000
0100000001000000010000000000000000000000000000000000000000000000
#include <iostream>
#include <bitset>
#include <climits>
#include <iomanip>

using namespace std;

template<class T>
auto toBitset(T x) -> bitset<sizeof(T) * CHAR_BIT>
{
    return bitset<sizeof(T) * CHAR_BIT>{ *reinterpret_cast<unsigned long long int *>(&x) };
}

int main()
{
    double x;
    while (cin >> x) {
        cout << setw(14) << x << " " << toBitset(x) << endl;
    }

    return 0;
}

https://wandbox.org/permlink/tCz5WwHqu2X4CV1E

遗憾的是,如果参数类型大于 unsigned long long 的大小,它将失败,例如 long double。这是 bitset 构造函数的限制。