Skip to content

Instantly share code, notes, and snippets.

@notfoundry
Last active November 24, 2018 19:05
Show Gist options
  • Select an option

  • Save notfoundry/940334616deb960b8d9ff958b16cf147 to your computer and use it in GitHub Desktop.

Select an option

Save notfoundry/940334616deb960b8d9ff958b16cf147 to your computer and use it in GitHub Desktop.
static_typeid
#include <cstddef>
namespace nonstd {
namespace detail {
template <class Tag, class Type = int, Type Start = 0, Type Step = 1>
class meta_counter {
template <Type N>
struct flag {
friend constexpr bool adl_flag (flag<N>);
};
template <Type N>
struct writer_impl {
friend constexpr bool adl_flag (flag<N>) { return true; }
static constexpr Type value = N;
};
template <Type N, bool = adl_flag(flag<N>{})>
static constexpr Type reader (int, flag<N>, Type r = reader(0, flag<N + Step>{})) {
return r;
}
template <Type N>
static constexpr Type reader(float, flag<N>) {
return N;
}
public:
static constexpr Type value(Type r = reader(0, flag<Start>{})){
return r;
}
template <Type Value = value()>
static constexpr Type next(Type r = writer_impl<Value>::value) {
return r + Step;
}
};
struct sti_hash_code_counter_tag {};
using sti_hash_code_counter = meta_counter<sti_hash_code_counter_tag, std::size_t>;
}
template <class T>
struct static_type_info {
using described_type = T;
template <class = void>
constexpr static std::size_t hash_code() {
return detail::sti_hash_code_counter::next();
}
template <class Other>
constexpr static bool before() {
return hash_code() < Other::hash_code();
}
};
}
template <class T>
using static_typeid = nonstd::static_type_info<T>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment