i want to find out the type of a Variable (the variable is given by a Template parameter, so I don't know what it is).
#include <iostream>
#include <typeinfo>
int main()
{
double test;
std::cout << typeid(test).name() << std::endl;
}
But the Code only emits: $./test
d
but I would need double instead.
The point is, I don't know which type to expect, but I have to write it in a subprogram, which hast to be compiled. So d is a bad idea.
If you know the list of types that must be supported, you can write your own function to do this:
template <typename T>
void printtype()
{
if (typeid(T) == typeid(double))
std::cout << "double";
else if (typeid(T) == typeid(int))
std::cout << "int";
}
Notice that since the function doesn't have an argument of type T, it must always have the type explicitly stated:
printtype<double>()
and of course, the type could be a parameter type:
printtype<U>()
In GNU ABI, there is a helper to demangle the name()
of a typeid
Disclaimer In case it wasn't obvious, of course the GNU ABI only supports demangling names from the GNU ABI (and probably not even wildly varying versions).
#include <cxxabi.h>
#include <stdlib.h>
#include <string>
template <typename T> std::string nameofType(const T& v)
{
int status;
char *realname = abi::__cxa_demangle(typeid(v).name(), 0, 0, &status);
std::string name(realname? realname : "????");
free(realname);
return name;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With