Why is std::bitset::size non-static

余生颓废 提交于 2019-12-08 18:48:40

问题


I can't possibly imagine why it was chose that std::bitset::size is non-static. It makes it much harder to get a constexpr size; you have to write something like this:

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;
}

When if it were static all you would have to do would be

BitsetType::size()

and there would be no sacrifice of functionality.

Is there a historical reason that I am missing or is there a technical fact I am missing?


回答1:


The assumption of a not constexpr std::bitset::size is incorrect:

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)


来源:https://stackoverflow.com/questions/33428441/why-is-stdbitsetsize-non-static

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!