Last active
August 27, 2020 17:52
-
-
Save osvimer/33e62c9d6c11b12511b640e494b65d00 to your computer and use it in GitHub Desktop.
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 <stdio.h> | |
#include <iostream> | |
#include <string> | |
#include <typeinfo> | |
#define Print(x) \ | |
std::cout << "\x1B[36m" #x "\x1B[33m := \x1B[32m" << x << "\x1B[0m" \ | |
<< std::endl | |
typedef const char (char_array_10_t) [10]; | |
typedef const char (&array_10_ref_t) [10]; | |
typedef const char (*array_10_ptr_t) [10]; | |
class Integer { | |
public: | |
Integer(int i) : m(i) {} | |
const int Plus(const int& n) const { return m + n; } | |
private: | |
const int m; | |
}; | |
typedef const int (Integer::*PlusFunc)(const int&) const; | |
// https://stackoverflow.com/questions/281818/unmangling-the-result-of-stdtype-infoname | |
#ifdef __GNUG__ | |
#include <cxxabi.h> | |
#include <cstdlib> | |
#include <memory> | |
std::string Demangle(const char* name) { | |
// some arbitrary value to eliminate the compiler warning | |
int status = -4; | |
// enable c++11 by passing the flag -std=c++11 to g++ | |
std::unique_ptr<char, void (*)(void*)> res{ | |
abi::__cxa_demangle(name, nullptr, nullptr, &status), std::free}; | |
return (status == 0) ? res.get() : name; | |
} | |
#else | |
// does nothing if not g++ | |
std::string Demangle(const char* name) { | |
return name; | |
} | |
#endif | |
std::string InfoName(const std::type_info& t) { | |
return Demangle(t.name()); | |
} | |
template <typename T> | |
std::string TypeName(const T& t) { | |
return InfoName(typeid(t)); | |
} | |
int main(int argc, const char* argv[]) { | |
char_array_10_t arr = {"123456789"}; | |
Print(arr); | |
Print(TypeName(arr)); | |
Print(TypeName(&arr)); | |
Print(InfoName(typeid(char_array_10_t))); | |
array_10_ref_t ref = arr; | |
Print(ref); | |
Print(TypeName(ref)); | |
Print(TypeName(&ref)); | |
Print(InfoName(typeid(array_10_ref_t))); | |
array_10_ptr_t ptr = &arr; | |
Print(*ptr); | |
Print(TypeName(ptr)); | |
Print(TypeName(&ptr)); | |
Print(InfoName(typeid(array_10_ptr_t))); | |
Print((typeid(arr) == typeid(ref))); | |
Print((typeid(&arr) == typeid(ptr))); | |
Print((typeid(arr) == typeid(*ptr))); | |
Print((typeid(char_array_10_t) == typeid(array_10_ref_t))); | |
Print((typeid(char_array_10_t) == typeid(array_10_ptr_t))); | |
Print(TypeName(ref)); | |
Print(InfoName(typeid(array_10_ref_t))); | |
PlusFunc plus = &Integer::Plus; | |
Print(TypeName(plus)); | |
Print(TypeName(Integer(1))); | |
Print((Integer(2).*plus)(3)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment