Metaprogramming:Type name to string

1. Description

MRPT provides a powerful template which allows one to obtain a string representation of any type:

{syntaxhighlighter brush: cpp}
template
struct TTypeName
{
static std::string get();
};
{/syntaxhighlighter}

This template relies solely on meta-programming techniques (recursive evaluation of templates and partial specializations), and has nothing to do with RTTI or compiler specific extensions.

The supported types are:

  • POD (Plain Old Data): int, float, double, etc...
  • Most MRPT classes: All those derived from CSerializable, plus the lightweight geometry structures.
  • STL containers.
  • std::pair.
  • Any arbitrarily complex combination of the above.

2. Examples

The complete example can be found in MRPT/samples/type_name.

{syntaxhighlighter brush: cpp}
#include
#include

using namespace mrpt;
using namespace mrpt::utils;
using namespace mrpt::poses;
using namespace mrpt::slam;
using namespace std;

...

cout << "Type: " << TTypeName::get() << endl;
cout << "Type: " << TTypeName::get() << endl;
cout << "Type: " << TTypeName::get() << endl;
cout << "Type: " << TTypeName::get() << endl;

cout << "Type: " << TTypeName >::get() << endl;
cout << "Type: " << TTypeName >::get() << endl;

cout << "Type: " << TTypeName > >::get() << endl;

cout << "Type: " << TTypeName

>::get() << endl;
cout << "Type: " << TTypeName > >::get() << endl;

cout << "Type: " << TTypeName > >::get() << endl;

cout << "Type: " << TTypeName > > >::get() << endl;
{/syntaxhighlighter}

And the output is:

{syntaxhighlighter brush: plain}
Type: int32_t
Type: double
Type: CPose2D
Type: COccupancyGridMap2D
Type: std::vector
Type: std::set
Type: std::set>
Type: std::pair
Type: std::pair>
Type: std::map>
Type: std::set>>
{/syntaxhighlighter}


Syndicate content

The Mobile Robot Programming Toolkit (MRPT) initiative (C) 2012