为什么std :: bitset :: size是非静态的

Rus*_*ene 7 c++ std bitset c++11

我无法想象为什么选择std::bitset::size非静态的.这使得获得constexpr尺寸变得更加困难; 你必须写这样的东西:

template<int val>
struct int_
{
   static const constexpr value = val;
};

template<size_t size>
auto getBitsetSizeIMPL(std::bitset<size>)
{
   return int_<size>{};
}

template<typename BitsetType>
constexpr size_t getBitsetSize()
{
    return decltype(getBitsetSizeIMPL(BitsetType{}))::value;
}
Run Code Online (Sandbox Code Playgroud)

如果它是静态的,你所要做的就是

BitsetType::size()
Run Code Online (Sandbox Code Playgroud)

并且不会牺牲功能.

是否有一个历史原因,我失踪或有一个我失踪的技术事实?

Che*_*Alf 1

not 的假设constexpr std::bitset::size是不正确的:

std::size_t size() const; // until C++11
constexpr std::size_t size();  // since C++11, until C++14
constexpr std::size_t size() const; // since C++14)
Run Code Online (Sandbox Code Playgroud)