Skip to content

Instantly share code, notes, and snippets.

@hkaiser
Last active March 4, 2019 01:34
Show Gist options
  • Save hkaiser/552d7d30b45062d5ea3aadd1d79fbb89 to your computer and use it in GitHub Desktop.
Save hkaiser/552d7d30b45062d5ea3aadd1d79fbb89 to your computer and use it in GitHub Desktop.
#include <iostream>
struct A
{
static constexpr int id = 0x1;
};
struct B
{
static constexpr int id = 0x2;
};
struct C
{
static constexpr int id = 0x4;
};
namespace detail
{
template <int id, typename ... Ts>
struct inheritance_chain_is_valid;
template <int id>
struct inheritance_chain_is_valid<id>
{
static constexpr bool value = true;
};
template <int id, typename T1, typename ... Ts>
struct inheritance_chain_is_valid<id, T1, Ts...>
{
static constexpr bool value =
(id < T1::id) && inheritance_chain_is_valid<T1::id, Ts...>::value;
};
}
template <typename T1, typename ... Ts>
struct inheritance_chain_is_valid
{
static constexpr bool value =
detail::inheritance_chain_is_valid<T1::id, Ts...>::value;
};
namespace detail
{
template <typename ... Ts>
struct mask_from_inheritance_chain;
template <>
struct mask_from_inheritance_chain<>
{
static constexpr int value = 0;
};
template <typename T1, typename ... Ts>
struct mask_from_inheritance_chain<T1, Ts...>
{
static constexpr int value =
T1::id | mask_from_inheritance_chain<Ts...>::value;
};
}
template <typename ... Ts>
struct mask_from_inheritance_chain
{
static constexpr int value =
detail::mask_from_inheritance_chain<Ts...>::value;
};
template <typename ... Ts>
struct E : Ts ...
{
static_assert(inheritance_chain_is_valid<Ts...>::value,
"inheritance_chain_is_valid<Ts...>::value");
static constexpr int mask =
mask_from_inheritance_chain<Ts...>::value;
};
// leave unimplemented
template <>
struct E<>;
int main()
{
std::cout << std::hex << "E<A>::mask: " << E<A>::mask << "\n";
std::cout << std::hex << "E<A, C>::mask: " << E<A, C>::mask << "\n";
std::cout << std::hex << "E<A, B, C>::mask: " << E<A, B, C>::mask << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment