I'm attempting to do the following:
Say I have a class, A, and I have a static member variable, static_bit_id, that is a bitset<128>. At compile time, I want to create an ID that is a one hot bit of my choosing using bitset. I have a function that does this for me onehotbits(int n). for example:
bitset<128> b = onehotbits(4) // 0....01000
At compile time I want to assign to a static member of class A in such a way:
//in A.h
class A{
public:
const static bitset<128> bits;
};
// in A.cpp
const bitset<128> A::bits = onehotbits(1);
Previously this pattern worked with a constexpr function that instead of bitset, took uint64_t's and shifted them. Now doing the same with bitsets violates constexpr rules since operator << for bitset< T > is not a constexpr.
I can't think of a way to accomplish this using constexpr, or in a namespace safe way in general. I can initialize bitset<128> with a string, but that violates constexpr.
I'm currently getting around this problem via:
const bitset<128> A::bits = bitset<128>(1) >> n;
which seems to violate DRIP, the only way to get rid of this problem appears to use a MACRO, which wouldn't be necessary if I could just use >> operator for bitset.
Note I want to avoid using other libraries for this, especially not boost, that is overkill.
Note, while my question is similar to Initializing c++ std::bitset at compile time it is not the same, since the solution there does not work for my problem (since I'm not simply using a literal, but a literal that would have to be created at compile time via some input)