6

I want to pass bitsets to a function. What size should I assign to the bitset parameter bits in the function prototype if the bitsets have different sizes?

For example:

bitset<3> a;
bitset<4> b;

void ABC(bitset<size> bits){
    ...
}

ABC(a);
ABC(b);
remcycles
  • 1,246
  • 13
  • 14
pcoder
  • 401
  • 1
  • 7
  • 18

2 Answers2

7

You can templatize the function taking bitset as argument.

template <size_t bitsetsize>
void ABC(bitset<bitsetsize> a) {
   ...
}

This templatized function would be generated by compiler only when you use it somewhere in your code. If you use this function for bitsets of different sizes, separate functions would be instantiated for once for each size. So you should take care to avoid code depending on any local state variables (static variables local to function) as the function instances are different.

It is advisable to use a reference or constant reference to avoid object copy.

template <size_t bitsetsize>
void ABC(const bitset<bitsetsize> &a) {
   ...
}

An alternative which may not be fit for your requirements is to use std::vector<bool> instead of std::bitset if possible.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • 1
    This doesn't help, you're adding one more layer of template over `std::bitset`, which again depends on compile-time constant – P0W May 26 '14 at 10:36
  • 1
    Size of `std::bitset` is anyways compile time constant, so compiler would bind the right function at compile time. – Mohit Jain May 26 '14 at 10:38
  • @P0W I think I could not understand your concern. – Mohit Jain Aug 23 '15 at 11:02
  • So, I was telling what if OP wants to create bitsets at _runtime_ and pass to his/her function ? – P0W Aug 26 '15 at 00:54
  • 1
    @P0W OK, thanks. My interpretation was, same function accepts bitset from different part of code which may be of different size. What should be the size of bitset in my function argument. – Mohit Jain Aug 26 '15 at 06:31
  • @Mohit Jain My function compiled fine. But After I added a call to the template function, I got an unresolved external symbol link error. How do you call the function w/o any error? Here is what I did: " bitset<5> bs5; ABC(bs5);" – Louis Apr 28 '17 at 18:18
  • @Louis Make sure this template function's definition is available to the caller. If you are using it in just one source file, put the template on top. If you are using template function in multiple files, move it to appropriate header file. – Mohit Jain Apr 29 '17 at 03:51
2

This is not possible with STL bitset.

std::bitset<N> template requires a fixed size in advance (at compile-time)

However, one way you can do this by using boost::dynamic_bitset


Something like following:

#include <iostream>
#include <boost/dynamic_bitset.hpp>

void ABC(boost::dynamic_bitset<> &a)
{
    /* for (boost::dynamic_bitset<>::size_type i = 0;
            i < a.size(); ++i)
          std::cout << a[i]; */
    std::cout << a << "\n";
}


int main()
{
    std::size_t size= 5; // take any value for 'size' at runtime

    boost::dynamic_bitset<> x(size); // all 0's by default
    x[0] = 1;
    x[1] = 1;
    x[4] = 1;
    ABC( x );

    return 0;
}

See here

P0W
  • 46,614
  • 9
  • 72
  • 119