'error: initializations for multiple members
I'm trying to create a constexpr union that sets the last 3 bits of its value according to template parameters. The idea is that the constructor first initializes the val variable to whatever is passed to the constructor and then the last three bits are set with template parameters and a bitfield. However, the compiler is complaining.
My code:
template<typename T, bool a, bool b, bool c>
union some_type
{
T val;
struct {
T empty_ : sizeof(T)*CHAR_BIT-3;
T a_ : 1;
T b_ : 1;
T c_ : 1;
} bit_type;
some_type(uint32_t var) : val{var}, bit_type{0,a,b,c} {}
};
int main()
{
some_type<uint32_t,1,1,0>(245878);
}
Error:
<source>:244:37: required from here
<source>:234:5: error: initializations for multiple members of 'some_type<unsigned int, true, true, false>'
234 | some_type(uint32_t var) : type{var}, bit_type{0,a,b,c} {}
| ^~~~~~~~~
Is there someway I can set the last three bits of a value in a constexpr way AND perserve the information of which bits were stored (in the above example by naming the bits a_, b_, c_ for later read operations)?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
