-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.hh
41 lines (31 loc) · 802 Bytes
/
base.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#pragma once
#include <cstdlib>
#include <memory>
#include <cxxabi.h>
#include <typeinfo>
namespace util {
// Calls an element destructor
template <class T>
void destruct (T& el) {
el.~T();
}
#ifdef __GNUG__
// Gets the name of T
// Thanks to https://stackoverflow.com/a/4541470/6441345
template <typename T>
std::string type_name (void) {
char const* name = typeid(T).name();
int status;
// Build a unique_ptr of the demangled name
std::unique_ptr<char, void(*)(void*)> res{
abi::__cxa_demangle(name, nullptr, nullptr, &status),
std::free
};
// Returns a string
return (status == 0) ? res.get() : name;
}
// Gets the name of T
template <typename T>
std::string type_name (T const&) { return type_name<T>(); }
#endif
};