Re: How to capture the name of a class in a string at compile time ?
From: josh (smileyfaceswillruletheworld_at_yahoo.com.NOSPAM)
Date: 07/07/04
- Next message: dover: ""endl and "\n""
- Previous message: Karl Heinz Buchegger: "Re: std::find_if, std::logical_or and short evaluation"
- In reply to: Alex Vinokur: "Re: How to capture the name of a class in a string at compile time ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 07 Jul 2004 07:55:00 GMT
Alex Vinokur wrote:
> "Vincent Cantin" <MY_EMAIL_IS_karma@astrocorp.com.tw> wrote in message news:2kvapqF6ok4gU1@uni-berlin.de...
>
> If you are using GNU g++ version 3.3
> the following method can be used.
[...]
> template<class T>
> string Container<T>::getClassIdentifier()
> {
> const string str0 (__PRETTY_FUNCTION__);
> string str1 (str0.substr (0, str0.find ("<")));
> str1 = str1.substr (str1.find_last_of (' ') + 1);
> string str2 (str0.substr (str0.find_last_of (' ') + 1));
> str2 = str2.substr (0, str2.size() - 1);
>
> return str1 + "<" + str2 + ">";
> }
Or you could go for something a little more generic:
(__FUNCSIG__ is msvc7 specific, I assume gcc's __PRETTY_FUNCTION__ would
be similar)
#include <iostream>
#include <string>
/*d::string typestring< <- must be same length */
std::string dummy(void) { return __FUNCSIG__; }
template <typename T>
std::string typestring(void)
{
std::string n = __FUNCSIG__ + dummy().size();
return n.erase(n.rfind('>'));
}
struct etc { };
template<class T> class Container { };
int main(void)
{
std::cout << typestring<int>() << '\n';
std::cout << typestring<std::string>() << '\n';
std::cout << typestring<etc>() << '\n';
std::cout << typestring<Container<Container<int> > >() << '\n';
}
Result:
int
class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >
struct etc
class Container<class Container<int> >
Personally, I'd probably go for something more tedious but standard.
-josh
- Next message: dover: ""endl and "\n""
- Previous message: Karl Heinz Buchegger: "Re: std::find_if, std::logical_or and short evaluation"
- In reply to: Alex Vinokur: "Re: How to capture the name of a class in a string at compile time ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|