0

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.

  • Does this answer your question? [Define bitset size at initialization?](https://stackoverflow.com/questions/3134718/define-bitset-size-at-initialization) Use `std::vector` – KamilCuk Apr 10 '20 at 15:07
  • What I didn't say, exactly I'm reading pixel array from a bitmap and I wanted store every pixel separately as a bitset with color depth size. For example I have 4-bit bitmap and every loop allocates new std::bitset<4> then it reads those bits and then it converse to RGB value. – Whatever playgames Apr 10 '20 at 15:10
  • I do not understand how is it related to the problem. If you convert some values to RGB, why bother with using bitset at all. – KamilCuk Apr 10 '20 at 15:12
  • I tried vector and it was slower a bit. dynamic_bitset should be ok, Thanks! – Whatever playgames Apr 10 '20 at 15:14

0 Answers0