Skip to content

Instantly share code, notes, and snippets.

@wuairc
Last active September 29, 2015 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuairc/6ab55850e65ea29e2142 to your computer and use it in GitHub Desktop.
Save wuairc/6ab55850e65ea29e2142 to your computer and use it in GitHub Desktop.
Demangle type name generated by gcc, and test various string prefix.
#include <iostream>
#include <iomanip>
#include <typeinfo>
#include <cxxabi.h>
static char *getDemangledName(const char *mangledName) {
int status;
return abi::__cxa_demangle(mangledName, 0, 0, &status);
}
int main() {
#define OUT(t) { \
char * demangled = getDemangledName(typeid(t).name()); \
std::cout << std::left << std::setw(12) << #t \
<< std::setw(16) << demangled \
<< "\tsize = " << sizeof(t) << std::endl; \
free(demangled); }
#define STR_(prefix) prefix ## "中文World"
#define STR(prefix) STR_(prefix)
#define TEST_(prefix, type) { auto prefix = STR(prefix); OUT(prefix); \
type arr_##prefix[] = STR(prefix); OUT(arr_##prefix); }
#define TEST(prefix, type) TEST_(prefix, type)
TEST(u8, const char);
TEST(u, const char16_t);
TEST(U, const char32_t);
TEST(L, const wchar_t);
return 0;
}
u8 char const* size = 8
arr_u8 char [12] size = 12
u char16_t const* size = 8
arr_u char16_t [8] size = 16
U char32_t const* size = 8
arr_U char32_t [8] size = 32
L wchar_t const* size = 8
arr_L wchar_t [8] size = 32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment