Giter VIP home page Giter VIP logo

statictypeinfo's Introduction

Actions Status Actions Status Actions Status Actions Status Actions Status codecov

StaticTypeInfo

A simple C++17 compile-time type info library.

API

The header-only library exposes two main constexpr functions: constexpr TypeName getTypeName<T>() and constexpr TypeIndex getTypeIndex<T>(). The TypeIndex may be used as a key in any hash-based container.

#include <static_type_info.h>

void example() {
  using namespace static_type_info;

  // compile-time type names
  constexpr auto name = getTypeName<int>();
  static_assert(name == "int");

  // compile-time type indices
  constexpr auto intIdx = getTypeIndex<int>();
  constexpr auto floatIdx = getTypeIndex<float>();
  static_assert(intIdx != floatIdx);

  // combine both in a single type
  constexpr auto typeID = getTypeID<int>();
  static_assert(typeID.name == "int");
  static_assert(typeID.index == getTypeIndex<int>());
}

Compatibility

The library has been tested with AppleClang 11, Visual Studio 16 2019, and gcc-9. Note that type names and indices are not compatible between compilers.

How it works

The type name is extracted from the macro __PRETTY_FUNCTION__ (clang/gcc) or __FUNCSIG__ (on MSVC) inside a probe function and converted to a string_view using the constexpr constructor. The type index is determined from the pointer to a static member of a templated string (clang / msvc) or from a 64 bit fnv1a hash of the type name (gcc and others).

How to integrate

Use CPM.cmake to easily add the headers to your CMake project.

CPMAddPackage(
  NAME StaticTypeInfo
  VERSION 1.3
  GIT_REPOSITORY https://github.com/TheLartians/StaticTypeInfo
)

target_link_libraries(myProject StaticTypeInfo)

Licence

The library is released under the MIT license. See the LICENSE for more info.

statictypeinfo's People

Contributors

thelartians avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.