Reputation: 38949
Is there a way to use an initializer_list
to construct a bitset
?
For example I'd like to do:
const auto msb = false;
const auto b = true;
const auto lsb = false;
const bitset<3> foo = {msb, b, lsb};
But when I try this I get:
error: could not convert
{msb, b, lsb}
from '<brace-enclosed initializer list>' toconst std::bitset<3u>
Do I have to use shifts to construct an unsigned long
to initialize foo
, or is there a way to do this that I don't know about?
Upvotes: 4
Views: 1659
Reputation: 98
Short example using string representation of bitset. In some cases this implementation may not be optimal.
template <typename ... args>
auto make_bitset(args... bools) -> std::bitset<sizeof...(bools)>
{
char data[] = {bools ? '1':'0'...};
return std::bitset<sizeof...(bools)>(data, sizeof...(bools));
}
int main()
{
std::cout << make_bitset(1,1,0,0,1,0, 1) << "\n";
std::cout << make_bitset(1,1,1) << "\n";
std::cout << make_bitset(0,0,0,0) << "\n";
}
Another implementation using "almost recursive" calls:
template <size_t N, int pos>
void init_bitset(std::bitset<N>&){}
template <size_t N, int pos = 0, typename ...Bits>
void init_bitset(std::bitset<N>& bitset, bool head, Bits ...tail)
{
bitset.set(pos, head);
init_bitset<N, pos + 1>(bitset, tail...);
}
template <typename ... args>
auto make_bitset(args... bools) -> std::bitset<sizeof...(bools)>
{
std::bitset<sizeof...(bools)> bitset;
init_bitset(bitset, bools...);
return bitset;
}
int main()
{
std::cout << make_bitset(1,1,0,0,1,0, 1) << "\n";
std::cout << make_bitset(1,1,1) << "\n";
std::cout << make_bitset(0,0,0,0) << "\n";
}
See compiler explorer for details https://godbolt.org/g/9z2HZa
bonus version using C++17
template <typename ... args>
auto make_bitset(args... bools)
{
unsigned n = 0;
static_assert( sizeof...(bools) <= sizeof(n) * 8, "overflow" );
((n = n * 2 + static_cast<bool>(bools)), ...);
return std::bitset<sizeof...(bools)>(n);
}
Upvotes: 3
Reputation: 69912
There is no constructor to directly construct a bitset from an initialiser list. You'll need a function:
#include <bitset>
#include <initializer_list>
#include <iostream>
auto to_bitset(std::initializer_list<bool> il)
{
using ul = unsigned long;
auto bits = ul(0);
if (il.size())
{
auto mask = ul(1) << (il.size() - 1);
for (auto b : il) {
if (b) {
bits |= mask;
}
mask >>= 1;
}
}
return std::bitset<3> { bits };
}
int main()
{
auto bs = to_bitset({true, false, true});
std::cout << bs << std::endl;
}
expected results:
101
As mentioned in comments, a variadic version is also possible.
#include <bitset>
#include <iostream>
#include <utility>
namespace detail {
template<std::size_t...Is, class Tuple>
auto to_bitset(std::index_sequence<Is...>, Tuple&& tuple)
{
static constexpr auto size = sizeof...(Is);
using expand = int[];
unsigned long bits = 0;
void(expand {
0,
((bits |= std::get<Is>(tuple) ? 1ul << (size - Is - 1) : 0),0)...
});
return std::bitset<size>(bits);
}
}
template<class...Bools>
auto to_bitset(Bools&&...bools)
{
return detail::to_bitset(std::make_index_sequence<sizeof...(Bools)>(),
std::make_tuple(bool(bools)...));
}
int main()
{
auto bs = to_bitset(true, false, true);
std::cout << bs << std::endl;
}
Upvotes: 5