I have the following code in C++17 in which I am defining a struct which is a bit mask and has member variables which are bit fields of type bool.
I am defining a tie function so that I can convert it to a comparable std::tuple object, which can be handy. Problem is: std::tie seems to be doing something wrong, and the value of first member variable seems to be false even if the default constructor has correctly initialized it to true.
Manually taking single references of bool to the bit fields seems to work as expected.
I am currently using CLang 12.
This is the code:
#include <cassert>
#include <tuple>
struct Bitmask
{
Bitmask( )
: first{true}
, second{false}
, third{false}
{
}
bool first : 1;
bool second : 1;
bool third : 1;
};
auto
tie( const Bitmask& self )
{
return std::tie( self.first, self.second, self.third );
}
int main()
{
Bitmask default_bitmask;
Bitmask custom_bitmask;
custom_bitmask.first = false;
custom_bitmask.second = true;
const bool& ref_1 = default_bitmask.first;
const bool& ref_2 = custom_bitmask.first;
assert( ref_1 != ref_2 ); // this check works
assert( std::get< 0 >( tie( default_bitmask ) ) == true ); // this check fails
assert( std::get< 0 >( tie( custom_bitmask ) ) == false ); // this check works
}