Skip to content

Instantly share code, notes, and snippets.

@mazgi
Created March 15, 2013 09:27
Show Gist options
  • Save mazgi/5168590 to your computer and use it in GitHub Desktop.
Save mazgi/5168590 to your computer and use it in GitHub Desktop.
c++で型取得してみる
#include <iostream>
#include <cxxabi.h>
template<class T>
void func(const T& t)
{
int status;
std::cout << "["
<< t << "]["
<< typeid(t).name() << "]["
<< abi::__cxa_demangle(typeid(t).name(), 0, 0, &status)
<< "]" << std::endl;
}
template<class First, class... Rest>
void func(const First& first, const Rest&... rest)
{
func(first);
func(rest...);
}
int main(int argc, const char * argv[])
{
func(1, 2, 0.0,
"", 'a', NULL,
"abc", "bcd", "bcd", "bcde",
std::string("str"), std::string("str"), std::string("str2"));
return 0;
}
@mazgi
Copy link
Author

mazgi commented Mar 19, 2013

char* を無理矢理判別するなら↓で一応できるっぽい。

                const char *CharArrayName = "char [";
                int status = 0;
                char *demangled = demangle__(typeid(value).name(), status);
                if (status == 0) {
                    if (strncmp(CharArrayName, demangled, strlen(CharArrayName)) == 0) {
                      // char*
                    }
                }
                free(demangled);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment