I'm writing a bitmap interpretation program in C++ and I want to use bitset as a buffer. Every bitmap has a different size, so bitset must be allocated dynamically.
First and the most stupid idea
size_t _alloc; std::cin >> _alloc; std::bitset<1> *Buff = new std::bitset<1>[_alloc];
It doesn't make sense because I could use
bool
.
After spending some time searching, I realized that I can use const_cast
.
Using
const_cast
.size_t _alloc; std::cin >> _alloc; std::bitset<const_cast<const size_t*>(&_alloc)> Buff; //or std::bitset<(const size_t*)&_alloc> Buff;
But it doesn't work very well 'this' cannot be used in the constant expression
. Is there any way do allocate bitset with non-const size? If not, Is there any alternative container?
I'm using Visual Studio 2019.