Created
November 12, 2016 22:09
-
-
Save tomilov/94c428efdeda54fdff2a68e935320e6c to your computer and use it in GitHub Desktop.
array to template arguments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <type_traits> | |
#include <utility> | |
#include <iterator> | |
#include <typeinfo> | |
#include <memory> | |
#include <mutex> | |
#include <string> | |
#include <cxxabi.h> | |
namespace | |
{ | |
std::mutex m; | |
std::unique_ptr< char, decltype(std::free) & > demangled_name{nullptr, std::free}; | |
std::size_t length = 0; | |
} | |
std::string | |
get_demangled_name(char const * const symbol) noexcept | |
{ | |
if (!symbol) { | |
return "<null>"; | |
} | |
std::lock_guard< std::mutex > lock(m); | |
int status = -4; | |
demangled_name.reset(abi::__cxa_demangle(symbol, demangled_name.release(), &length, &status)); | |
return ((status == 0) ? demangled_name.get() : symbol); | |
} | |
template< std::size_t size, typename = std::make_index_sequence< size > > | |
struct iterate; | |
template< std::size_t size, std::size_t ...indices > | |
struct iterate< size, std::index_sequence< indices... > > | |
{ | |
template< class F > | |
constexpr | |
auto | |
operator () (F f) const | |
{ | |
return f(std::integral_constant< std::size_t, indices >{}...); | |
} | |
}; | |
template< auto const & value, typename char_type = std::remove_all_extents_t< std::remove_reference_t< decltype(value) > > > | |
constexpr auto constexpr_string = iterate< std::size(value) >{}([] (auto... i) constexpr { return std::integer_sequence< char_type, value[i]... >{}; }); | |
template< typename char_type, char_type ...chars > | |
constexpr | |
std::integer_sequence< char_type, chars... > | |
operator ""_s () | |
{ | |
return {}; | |
} | |
template< typename type, std::size_t size, std::size_t ...indices > | |
std::initializer_list< type > | |
make_initializer_list(type const (& value)[size], std::index_sequence< indices... >) | |
{ | |
return {value[indices]...}; | |
} | |
template< typename type, std::size_t size > | |
std::initializer_list< type > | |
make_initializer_list(type const (&value)[size]) | |
{ | |
return make_initializer_list< type, size >(value, std::make_index_sequence< size >{}); | |
} | |
constexpr char s[] = "\x03\x02\x01"; | |
#include <iostream> | |
int main() | |
{ | |
std::cout << get_demangled_name(typeid(constexpr_string< s >).name()) << std::endl; | |
std::cout << get_demangled_name(typeid(make_initializer_list("123")).name()) << std::endl; | |
std::cout << get_demangled_name(typeid("\x03\x02\x01"_s).name()) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment