Last active
September 1, 2020 12:24
-
-
Save ZhekehZ/8505cd2993a9a98add600f8a8df5b162 to your computer and use it in GitHub Desktop.
Returns the number of elements in the structure (type) that are applicable for structured binding declaration (https://en.cppreference.com/w/cpp/language/structured_binding). On godbolt: https://godbolt.org/z/Px3Pbb
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 <utility> | |
#include <tuple> | |
#include <boost/preprocessor/repetition/repeat.hpp> | |
#include <boost/preprocessor/punctuation/comma_if.hpp> | |
#include <boost/preprocessor/control/if.hpp> | |
#define BINDERS_LIMIT 256 // Number of binders must be less than BINDERS_LIMIT | |
template <typename... Ts> struct overloads : Ts... { using Ts::operator()...; }; | |
template <typename... Ts> overloads(Ts...) -> overloads<Ts...>; | |
template <typename Cls> | |
auto count_binders_ptr() noexcept { | |
#define DECL(z, n, d) char (*_##n)[n] = nullptr; | |
BOOST_PP_REPEAT(BINDERS_LIMIT, DECL, _) | |
#undef DECL | |
Cls * cls = nullptr; | |
#define DECL(z, n, d) BOOST_PP_COMMA_IF(n) _##n | |
#define LAM(n) , [&](auto && t) -> \ | |
decltype(({auto && [BOOST_PP_REPEAT(n, DECL, int)] = t; _##n;})) \ | |
{ return _##n; } | |
#define Z(...) [&](...) { return _0; } | |
#define LAMBDA_GENERATOR(z, n, d) BOOST_PP_IF(n, LAM, Z)(n) | |
return overloads { BOOST_PP_REPEAT(BINDERS_LIMIT, LAMBDA_GENERATOR, _) } (*cls); | |
#undef LAM | |
#undef DECL | |
#undef Z | |
#undef LAMBDA_GENERATOR | |
} | |
template <typename Cls> | |
static constexpr std::size_t n_bindings = sizeof(*count_binders_ptr<Cls>()); | |
/// USAGE | |
using T0 = int; | |
struct T1 { char c; }; | |
using T2 = std::pair<int, int>; | |
struct T3 { int x; int y; int z; }; | |
using T10 = std::tuple<int, int, int, int, int, int, int, int, int, int>; | |
static_assert(n_bindings<T0> == 0); | |
static_assert(n_bindings<T1> == 1); | |
static_assert(n_bindings<T2> == 2); | |
static_assert(n_bindings<T3> == 3); | |
static_assert(n_bindings<T10> == 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Warning: there is a bug in
gcc
g++-10 output: