简体   繁体   中英

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 . Can anyone please explain me that? Does that mean a bitset can never be null?

sizeof can never return zero in C++. 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. The std::bitset<N> class provides an interface to a sequence of N bits. If N = 0, the standard guarantees that this interface is to exactly 0 bits. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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