S
Severin Ecker
Hi,
I'm not aware of any solution but hopefully there is a way that I just
don't know.
Can I get the name of a type as string (literal) in a consistent
platform independent way?
Since I only need this information statically I could use (and prefer) a
compile time possibility (i.e.: I won't need to query the type name of
an object at runtime), basically it's something like:
template <class T>
struct TypeName
{
static const char* name = //somehow get the name of T;
};
so
TypeName<int>::name == "int";
and
struct bla {};
TypeName<bla>::name == "bla";
I know that the type_info::name is implementation dependent so that's
not an option (additionally it doesn't even have to be related to the
name you type in the code as far as I'm aware of as long as it's
unambiguous) but since the compiler has all the knowledge that I need I
was hoping that there's a way to get the info from it.
In essence what I'm trying/needing to do (and before you ask, yes I do
need the thing as string and can't just let the type system figure it
out for me) is the following (and I really would like to save the
tedious work of dozens of specializations for all the types that can
actually be used for Foo):
template <class T> std::string GetCast(T t) { return ""; };
template <class T> std::string GetCast(T* t)
{
std::string cast("(");
cast += std::string(TypeName<T>::name) + ")";
return cast;
}
my thanks in advance.
cheers,
severin
I'm not aware of any solution but hopefully there is a way that I just
don't know.
Can I get the name of a type as string (literal) in a consistent
platform independent way?
Since I only need this information statically I could use (and prefer) a
compile time possibility (i.e.: I won't need to query the type name of
an object at runtime), basically it's something like:
template <class T>
struct TypeName
{
static const char* name = //somehow get the name of T;
};
so
TypeName<int>::name == "int";
and
struct bla {};
TypeName<bla>::name == "bla";
I know that the type_info::name is implementation dependent so that's
not an option (additionally it doesn't even have to be related to the
name you type in the code as far as I'm aware of as long as it's
unambiguous) but since the compiler has all the knowledge that I need I
was hoping that there's a way to get the info from it.
In essence what I'm trying/needing to do (and before you ask, yes I do
need the thing as string and can't just let the type system figure it
out for me) is the following (and I really would like to save the
tedious work of dozens of specializations for all the types that can
actually be used for Foo):
template <class T> std::string GetCast(T t) { return ""; };
template <class T> std::string GetCast(T* t)
{
std::string cast("(");
cast += std::string(TypeName<T>::name) + ")";
return cast;
}
my thanks in advance.
cheers,
severin