简体   繁体   English

C ++-std :: bitset <0>的大小?

[英]c++ - size of std::bitset<0>?

I thought the size of std::bitset<0> myBS would be 0 , while std::cout << sizeof(myBS) print out 1 . 我认为std::bitset<0> myBS的大小为0 ,而std::cout << sizeof(myBS)打印出1 Can anyone please explain me that? 谁能解释一下吗? Does that mean a bitset can never be null? 这是否意味着一个位集永远不能为空?

sizeof can never return zero in C++. 在C ++中, sizeof永远不会返回零。 An object's size is at least one (except in the case of empty base classes). 一个对象的大小至少为一个(空基类除外)。 If this were not the case, then you could have an array of objects where all the objects would be stored at the same address. 如果不是这种情况,那么您可以拥有一个对象数组,其中所有对象都将存储在同一地址。

The value of sizeof(std::bitset<N>) is actually irrelevant, anyway. 无论如何, sizeof(std::bitset<N>)值实际上是不相关的。 The std::bitset<N> class provides an interface to a sequence of N bits. std::bitset<N>类为N个序列提供接口 If N = 0, the standard guarantees that this interface is to exactly 0 bits. 如果N = 0,则标准保证该接口精确到0位。 This is true no matter how large the object is. 无论对象有多大,都是如此。 No matter how many bits are in that object, you can only use 0 of them. 无论该对象中有多少位,您都只能使用其中的0位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM