Created
December 22, 2015 12:58
-
-
Save artemkin/928cc72e8bfd75b91911 to your computer and use it in GitHub Desktop.
Boolean pack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <type_traits> | |
namespace detail | |
{ | |
constexpr std::size_t pack_bool(std::size_t result) | |
{ | |
return result; | |
} | |
template<typename T, typename... Ts> | |
constexpr std::size_t pack_bool(std::size_t result, T arg, Ts... args) | |
{ | |
static_assert(std::is_same<bool, T>::value, "boolean expected"); | |
return pack_bool((result << 1) | arg, args...); | |
} | |
} | |
template<typename T, typename... Ts> | |
constexpr std::size_t pack_bool(T arg, Ts... args) | |
{ | |
static_assert(std::is_same<bool, T>::value, "boolean expected"); | |
return detail::pack_bool(arg, args...); | |
} | |
int main() { | |
auto a = pack_bool(3 > 5, false, false); | |
auto b = pack_bool(false, false, true); | |
auto c = pack_bool(false, true, false); | |
auto d = pack_bool(false, true, true); | |
auto e = pack_bool(true, false, false); | |
auto f = pack_bool(true, false, true); | |
auto g = pack_bool(true, true, false); | |
auto h = pack_bool(true, true, true); | |
std::cout << a << std::endl; | |
std::cout << b << std::endl; | |
std::cout << c << std::endl; | |
std::cout << d << std::endl; | |
std::cout << e << std::endl; | |
std::cout << f << std::endl; | |
std::cout << g << std::endl; | |
std::cout << h << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment