Skip to content

Add type_key<T>() to generate a unique key for a type #1249

@jwellbelove

Description

@jwellbelove

The key returned from type_key<T>() must be unique for each type within an executable.

Possible implementation:-

#if ETL_USING_CPP11
using type_key_t = void*;

template <typename T>
struct type_anchor
{
  static constexpr int value = 0;
};

template <typename T>
constexpr const void* type_key() noexcept
{
  return &type_anchor<T>::value;
}
#else
// For C++98/03
typedef void* type_key_t;

template <typename T>
const void* type_key()
{
  static char anchor; // unique per T
  return &anchor;
}
#endif

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    To do

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions