views:

320

answers:

5

This needs only work in g++.

I want a function

template<typename T> std::string magic();

such that:

Class Foo{}; magic<Foo>(); // returns "Foo";
Class Bar{}; magic<Bar>(); // returns "Bar";

I don't want this to be done via specialization (i.e. having to define magic for each type. I'm hoping to pull some macro/template black magic here. Anyone know how?)

Thanks!

+3  A: 

The stringizing operator on macros may be what you're looking for:

#define MAGICCLASSNAME(str) std::string magic(#str)
class Foo{}; MAGICCLASSNAME(foo)
David Gladfelter
+4  A: 

Try typeid(Foo).name() for a start. Parse as you see fit; will be implementation-dependent (but simply getting a string back is portable).

Potatoswatter
typeid(Foo).name() will return a mangled name. (e.g 3Foo)
Vijay Mathew
Parse as you see fit, eg: `char *n = typeid(Foo).name(); while ( *n `
Potatoswatter
+1  A: 

I have come up with the below:

#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;
class Foo{}; 
class Bar{};

template<typename T> 
inline std::string magic(const T& obj)
{
 return typeid(obj).name();
}

int main()
{
 Foo a;
 cout << magic<Foo>(a); // returns "Foo";
}

I tested this with g++ and works well.

Also I got it from this SO answer.

ardsrk
+7  A: 

To convert a type (or other identifer) into a string you need a macro, but a macro can not check if it's parameter is a valid type. To add type checking a template function can be added to the macro:

template<typename T>
std::string magic_impl(const char *name) { return name; }

#define more_magic(a) magic_impl<a>(#a)
#define magic(a) more_magic(a)

Here magic(int) gives the string "int" while magic(Foo) gives a "‘Foo’ was not declared" error if there is no such class.

sth
Of all the answers given here I like this answer the best because a) It does typechecking b) The result is the class name and not any mangled form which may be compiler dependent.
ardsrk
@ardsrk: typechecking? It checks that the string returned names a type in local scope, but it can't for example resolve a class from a typedef or a template argument.
Potatoswatter
I still don't understand the purpose of `more_magic`...
Matthieu M.
more_magic exists just to check that a type named "a" is declared and is in scope. Without that I could call magic(%%%%) and get the call to succeed even though %%%% couldn't have been a legal name for a type.
ardsrk
A: 

In C++ probably you are looking template black magic. If so, Modern C++ Design will be helpful.

Andy