Drake
NiceTypeName Class Reference

Obtains canonicalized, platform-independent, human-readable names for arbitrarily-complicated C++ types. More...

#include <drake/common/nice_type_name.h>

Static Public Member Functions

template<typename T >
static const std::string & Get ()
 Attempts to return a nicely demangled and canonicalized type name that is the same on all platforms, using Canonicalize(). More...
 
template<typename T >
static std::string Get (const T &thing)
 Returns the type name of the most-derived type of an object of type T, typically but not necessarily polymorphic. More...
 
static std::string Demangle (const char *typeid_name)
 Using the algorithm appropriate to the current compiler, demangles a type name as returned by typeid(T).name(), with the result hopefully suitable for meaningful display to a human. More...
 
static std::string Canonicalize (const std::string &demangled_name)
 Given a compiler-dependent demangled type name string as returned by Demangle(), attempts to form a canonicalized representation that will be the same for any compiler. More...
 

Detailed Description

Obtains canonicalized, platform-independent, human-readable names for arbitrarily-complicated C++ types.

Usage:

// For types:
using std::pair; using std::string;
using MyVectorType = pair<int,string>;
std::cout << "Type MyVectorType was: "
<< drake::NiceTypeName::Get<MyVectorType>() << std::endl;
// Output: std::pair<int,std::string>
// For expressions:
std::unique_ptr<AbstractThing> thing; // Assume actual type is ConcreteThing.
std::cout << "Actual type of 'thing' was: "
<< drake::NiceTypeName::Get(*thing) << std::endl;
// Output: ConcreteThing

We demangle and attempt to canonicalize the compiler-generated type names as reported by typeid(T).name() so that the same string is returned by all supported compilers and platforms. The output of NiceTypeName::Get<T>() is useful in error and log messages and testing. It also provides a persistent, platform-independent identifier for types; std::type_info cannot provide that.

Warning
Don't expect usable names for types that are defined in an anonymous namespace or for function-local types. Names will still be produced but they won't be unique, pretty, or compiler-independent.

This class exists only to group type name-related static methods; don't try to construct an object of this type.

Member Function Documentation

string Canonicalize ( const std::string &  demangled_name)
static

Given a compiler-dependent demangled type name string as returned by Demangle(), attempts to form a canonicalized representation that will be the same for any compiler.

Unnecessary spaces and superfluous keywords like "class" and "struct" are removed. The NiceTypeName::Get<T>() method uses this function to produce a human-friendly type name that is the same on any platform.

Here is the caller graph for this function:

string Demangle ( const char *  typeid_name)
static

Using the algorithm appropriate to the current compiler, demangles a type name as returned by typeid(T).name(), with the result hopefully suitable for meaningful display to a human.

The result is compiler-dependent.

See also
Canonicalize()

Here is the caller graph for this function:

static const std::string& Get ( )
inlinestatic

Attempts to return a nicely demangled and canonicalized type name that is the same on all platforms, using Canonicalize().

This is an expensive operation but is only done once per instantiation of NiceTypeName::Get<T>() for a given type T. The returned reference will not be deleted even at program termination, so feel free to use it in error messages even in destructors that may be invoked during program tear-down.

Here is the call graph for this function:

Here is the caller graph for this function:

static std::string Get ( const T &  thing)
inlinestatic

Returns the type name of the most-derived type of an object of type T, typically but not necessarily polymorphic.

This must be calculated on the fly so is expensive whenever called, though very reasonable for use in error messages. For non-polymorphic types this produces the same result as would Get<decltype(thing)>() but for polymorphic types the results will differ.

Here is the call graph for this function:


The documentation for this class was generated from the following files: