0

I have a problem with understanding of std::bitset. I want to use bitset as a switch for mutliple functions.

Imagine class A and function Func1. Constructor looks like this A(bool procFunc). Usage is simple. If I want to use Func1 i will pass true, otherwise false. Easy. I have a multiple choices and one bool is not enough so i want to use std::bitset.

Very very simple draft:

class A {
public:
   A();                     // default ctor
   A(bool procFunc);        // parameterized ctor with bool
   A(std::bitset<N>& bs);   // parameterized ctor with bitset
private:
   bool _procFunc;
   std::bitset<4> _bs;

   void Func1();
   void Func2();
   void Func3();
   void Func4();
};

A:A() {}

A:A(bool procFunc) :
   _procFunc(procFunc)
{}

A:A(std::bitset<N>& bs) :
   _bs(bs)
{}

If I had only Func1. I would switch on this way:

   if(_procFunc == true){
      Func1();
   }

But I dont know how to exactly use std::bitset for multiple choices. I want to define some enum or sth similar and define 0001 calls Func1() ... 0010 calls Func2() ... 0100 calls Func2() ... 1000 calls Func4().

Thanks for any help.

AxelF93
  • 43
  • 5
  • 2
    What happens if the bitset has the value `0111`? Do you want to call three functions in that case? Also, do you know that you can access the individual bits in a `std::bitset` with [square brackets `[]`](https://en.cppreference.com/w/cpp/utility/bitset/operator_at)? – Max Langhof Sep 10 '19 at 15:42
  • 1
    There is `operator |` for `std::bitset`... – Jarod42 Sep 10 '19 at 15:45
  • 1
    To me it seems like a design problem, and your implementation shouldn't use a flag at all. Perhaps using templates and specialization? Or inheritance? Or "tag" arguments? And what is the *real* problem you need to solve? What is the requirements you have that made you come up with this design? Right now this is to much of an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) IMO. – Some programmer dude Sep 10 '19 at 15:45
  • I want to use only one function of these. Yes I know I can access this way. – AxelF93 Sep 10 '19 at 15:45
  • If you want to call only one function then why you want to use bitset and don't want to use int with number of function to call? – Öö Tiib Sep 10 '19 at 15:51
  • 1
    `I want to use only one function of these` – then rather consider an enum. Especially: you have *names* for whatever you need to initialise. And even *if* you wanted to be able to call multiple functions, I still wouldn't use a `std::bitset`, either separate bool parameters or a struct collecting all parameters as *named* boolean members – albeit requiring more space; *if* space still is an issue, you might use a classic bit field (i. e. not `std::bitset`, but e. g. `uint32_t param1 : 1; uint32_t param2 : 1;). In any case, you have *names* for what you want to do instead of relying on indices. – Aconcagua Sep 10 '19 at 17:02
  • Side note: it's possible to have boolean bit fields: `bool b0 : 1, b1 : 1, b2 : 1;`... – Aconcagua Sep 10 '19 at 17:14

0 Answers0