C++ 为什么std::bitset::size是非静态的
我无法想象为什么选择C++ 为什么std::bitset::size是非静态的,c++,c++11,std,bitset,C++,C++11,Std,Bitset,我无法想象为什么选择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>{
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;
}
而且不会牺牲功能
我遗漏的是历史原因还是技术事实?假设非
constexpr
是不正确的:
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)
FWIW,您可以执行
BitsetType{}.size()
.True!我没想过。那好多了。但是,还有什么原因使它不能是静态的吗?我想这和std::array::size
不是静态的原因是一样的。上周有人问起了这件事。@lightness To what declutation?@RussellGreene:我不记得了。因此,如果您查找最近的相关问题,您会发现:PBut为了使用这些问题,您必须有一个constexpr位集。这不是静态的,你没有回答这个问题。我知道那是康斯特普。我只是好奇为什么他们会选择那个设计选项。@RussellGreene:我不知道,但创建实例的两个字符,{}
或()
,实际上并没有太大的符号开销。只是有点不完美,我完全同意。我只是好奇它不是静态的是否有什么原因:对于相同的类型,它总是返回相同的值。@RussellGreene:你可能有什么发现。与std::array
相同,只是其中std::tuple_size
起到了自由大小函数的作用。对于std::bitset
,它没有重载。好奇。
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)