Type safe enum bit flags

前端 未结 5 614
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-11 12:32

I\'m looking to use a set of bit flags for my current issue. These flags are (nicely) defined as part of an enum, however I understand that when you OR

5条回答
  •  时光说笑
    2021-01-11 13:33

    If you think about type safety, it is better to use std::bitset

    enum BITS { A, B, C, D }; 
    std::bitset<4> bset, bset1;
    bset.set(A); bset.set(C);
    bset1[B] = 1;
    assert(bset[A] == bset[C]);
    assert(bset[A] != bset[B]);
    assert(bset1 != bset);
    

提交回复
热议问题