I was writing a function that needed to return multiple variables, one of them being a bitset. Then I encountered some strange compilation error.
I tried different compilers, they all produce an error, albeit with different messages.
I tried googling, it seems that it has something to do with public and private inheritance. But I don't think it should affect this piece of code in any way.
The simplified code (C++17) looks like this:
#include <bitset>
auto f() { return bitset<100>(); }
int main()
{
auto [a] = f();
return 0;
}
If I remove the square brackets (i.e. remove the structured binding and use normal auto), it works.
The error messange is as follows:
source.cpp: In function 'int main()':
source.cpp:9:18: error: 'std::_Base_bitset<2>' is an inaccessible base of 'std::bitset<100>'
9 | auto [a] = f();
| ^
Compiler returned: 1
So my question is: Is this expected behaviour or am I doing something wrong? Suggestions are welcome.
There are three kinds of types that work with structured bindings:
tuple_size and tuple_element and overloads of get)std::bitset is none of these. Its specification does not say what its members are, and it does not provide an opt-in to the tuple protocol. Hence, it does not work with structured bindings. It is expected that auto [a] = f(); would fail.
If anything, why would a bitset<100> only provide a single binding? I would expect that if it provided bindings, it would provide 100 of them...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With