My bitset

For practice, I tried making my own bitset. I noticed, that the STL bitset has two operator[] overloads, a const one that returns a bool, and a non const one that returns a reference object. I attempted to do the same thing, but only the one that returns a reference object seems to be called, even for something as simple as:
bool b = myBitset[3];

I was able to make a crude workaround by adding a bool cast operator to the reference object, but I would like to know how to properly implement both [] operators.
Last edited on
The const one will only be called on a const object. In all other cases the non-const one will be called.

So write a function

1
2
3
4
5
template< typename T, size_t N >
void test_bracket( const my_bitset<T,N>& my ) {
    // Calls operator[] const
    std::cout << std::boolalpha << my[ 3 ] << std::endl;
}

Thanks.
Also, I want to use a non-dynamic array in the structure, and be able to set this using the number of bits in a template, the problem is I don't know how to convert the bits to bytes without having it found dynamically, unless I do this funky thing:
 
bytes[bits / 8 + int(bool(bits % 8))];

I assume there must be a better way
N / 8 + !!( N % 8 )

!!x is a clever programming trick, given an integer x, to convert 0 to 0 and non-zero to 1.
Topic archived. No new replies allowed.