scmg
scmg

Reputation: 1894

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?

Upvotes: 1

Views: 892

Answers (1)

Brian Bi
Brian Bi

Reputation: 119034

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.

Upvotes: 4

Related Questions