Skip to content

Instantly share code, notes, and snippets.

@alexpolt
Last active October 17, 2017 17:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpolt/532b48b9353e98e276b79296ec9f4ab6 to your computer and use it in GitHub Desktop.
Save alexpolt/532b48b9353e98e276b79296ec9f4ab6 to your computer and use it in GitHub Desktop.
C++ String Interning Code courtesy of Ruslan Abdikeev https://twitter.com/aruslan
//Proposal N3599 by Richard Smith "Literal operator templates for strings".
//Clang and GCC implement as an extension, MSVC doesnt imlement it
//more details are in the blog post http://alexpolt.github.io/intern.html
#include <cstddef>
#include <type_traits>
#include <utility>
template<char ...S> struct interned {
static constexpr char value[] = { S..., 0 };
};
template<char ...S> constexpr char interned<S...>::value[];
template<typename charT, charT ...S> constexpr const char *operator""_intern() {
static_assert(std::is_same<charT, char>(), "string char type mismatch");
return interned<S...>::value;
}
template<typename charT, charT ...S> constexpr auto operator""_intern_t() {
static_assert(std::is_same<charT, char>(), "string char type mismatch");
return interned<S...>{};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment