I try to extend the boost::dynamic_bitset
class with some functionality I need, such as counting bits after an AND operation, etc. Also I need access to the private members (m_num_bits, etc) because I want to be able to "override" the set()
method to ensure the bitset's capacity with the resize() fct, if the pos
of the bit to set is greater then the current bitset capacity. If I use the native set()
fct it throughs an assertion error in that case (because the set()
method does not resize in such case)
I am not very experienced with templates, also I am just getting back into C++ since a few weeks, it's a bit rusty, maybe somebody can help me out.
I started doing this:
template <typename Block, typename Allocator>
class ExtendedBitSet : public boost::dynamic_bitset<Block, Allocator> {
typedef boost::dynamic_bitset<Block, Allocator> super;
public:
void set(std::size_t pos) {
if (pos > super::size())
super::resize(pos);
set(pos);
}
bool get(std::size_t pos) const {
return super::test(pos);
}
};
// ...
ExtendedBitSet<> * bs = new ExtendedBitSet<>(128);
bs->set(33);
// ...
std::wcout << "Bit 48 is " << ((bs->get(48) == true) ? "true" : "false") << std::endl;
// ...
delete bs;
Ofcourse this is not compiling, the error is:
dynamic_bitset.cpp: In function ‘int main(int, char**)’:
dynamic_bitset.cpp:50: error: wrong number of template arguments (0, should be 2)
dynamic_bitset.cpp:7: error: provided for ‘template<class T, class Allocator> class ExtendedBitSet’
dynamic_bitset.cpp:50: error: invalid type in declaration before ‘=’ token
dynamic_bitset.cpp:50: error: wrong number of template arguments (0, should be 2)
dynamic_bitset.cpp:7: error: provided for ‘template<class T, class Allocator> class ExtendedBitSet’
dynamic_bitset.cpp:51: error: request for member ‘set’ in ‘* bs’, which is of non-class type ‘int’
..
dynamic_bitset.cpp:57: error: request for member ‘get’ in ‘* bs’, which is of non-class type ‘int’
Can somebody give a hint how to get that running? Also is there maybe a better way than deriving from the dynamic_bitset class or is this OK to do?
Any help is greatly appreciated.
Your ExtendedBitSet<>
has no default parameters for the template type parameters and your code is trying to instantiate it without specifying them explicitly. Try using ExtendedBitSet<unsigned, std::allocator<unsigned> > bs
instead.