472,353 Members | 996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

How to capture the name of a class in a string at compile time ?

I have a class defined by a template which needs to "say" its type to the
user via string.

As an example, here is the class that I want to fix :

template<class T> class Container : public Serializable
{
public:

T value;

static string classIdentifier;

string& getClassIdentifier();

};

template <class T> string& Container<T>::getClassIdentifier()
{
return classIdentifier;
}
For now, the only way I found to initiliaze the string is with :

string Container<bool>::classIdentifier("Container<bool>" );
string Container<char>::classIdentifier("Container<char>" );
string Container<short>::classIdentifier("Container<short >");
string Container<int>::classIdentifier("Container<int>");
string Container<float>::classIdentifier("Container<float >");
string Container<double>::classIdentifier("Container<doub le>");
string Container<unsigned char>::classIdentifier("Container<unsigned
char>");
string Container<unsigned short>::classIdentifier("Container<unsigned
short>");
string Container<unsigned int>::classIdentifier("Container<unsigned int>");
string Container<unsigned float>::classIdentifier("Container<unsigned
float>");
string Container<unsigned double>::classIdentifier("Container<unsigned
double>");

This solution works for the basis types, but now imagine that I would like
to use my template class with another type which is not in the list ? The
compiler will say that my static variable is not defined :-(

I think there might be a way to do it with a macro but I don't know how to
do. Does someone here have a solution ?

Vincent
Jul 22 '05 #1
3 2446

"Vincent Cantin" <MY***************@astrocorp.com.tw> wrote in message
news:2k************@uni-berlin.de...
I have a class defined by a template which needs to "say" its type to the
user via string.

As an example, here is the class that I want to fix :

template<class T> class Container : public Serializable
{


There is no guaranteed way of doing this, but the following might work on
some compilers

#include <type_info>

template<class T> class Container : public Serializable
{
public:
std::string getClassIdentifier() const
{
return typeid(T).name();
}
...

The problem is that the C++ standard does not make any guarantees as to what
the name() method returns, but some compilers implement it to return a human
readable name.

john
Jul 22 '05 #2

"Vincent Cantin" <MY***************@astrocorp.com.tw> wrote in message news:2k************@uni-berlin.de...
I have a class defined by a template which needs to "say" its type to the
user via string.

As an example, here is the class that I want to fix :

template<class T> class Container : public Serializable
{
public:

T value;

static string classIdentifier;

string& getClassIdentifier();

};

template <class T> string& Container<T>::getClassIdentifier()
{
return classIdentifier;
}
For now, the only way I found to initiliaze the string is with :

string Container<bool>::classIdentifier("Container<bool>" );
string Container<char>::classIdentifier("Container<char>" );
string Container<short>::classIdentifier("Container<short >");
string Container<int>::classIdentifier("Container<int>");
string Container<float>::classIdentifier("Container<float >");
string Container<double>::classIdentifier("Container<doub le>");
string Container<unsigned char>::classIdentifier("Container<unsigned
char>");
string Container<unsigned short>::classIdentifier("Container<unsigned
short>");
string Container<unsigned int>::classIdentifier("Container<unsigned int>");
string Container<unsigned float>::classIdentifier("Container<unsigned
float>");
string Container<unsigned double>::classIdentifier("Container<unsigned
double>");

This solution works for the basis types, but now imagine that I would like
to use my template class with another type which is not in the list ? The
compiler will say that my static variable is not defined :-(

I think there might be a way to do it with a macro but I don't know how to
do. Does someone here have a solution ?

Vincent


If you are using GNU g++ version 3.3
the following method can be used.

========= C++ code : foo.cpp : BEGIN =========
#include <string>
#include <iostream>
using namespace std;

template<class T>
class Container
{
public:

static const string classIdentifier;
static string getClassIdentifier();
};

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 + ">";
}

template<class T>
const string Container<T>::classIdentifier(Container<T>::getCla ssIdentifier());

class Foo {};
template<class T>
class Bar {};
int main ()
{
cout << Container<bool>::classIdentifier << endl;
cout << Container<int>::classIdentifier << endl;
cout << Container<Foo>::classIdentifier << endl;
cout << Container<Bar<int> >::classIdentifier << endl;
cout << Container<Bar<Foo> >::classIdentifier << endl;

cout << endl;
cout << Container<bool>().classIdentifier << endl;
cout << Container<int>().classIdentifier << endl;
cout << Container<Foo>().classIdentifier << endl;
cout << Container<Bar<int> >().classIdentifier << endl;
cout << Container<Bar<Foo> >().classIdentifier << endl;
return 0;
}
========= C++ code : foo.cpp : END ===========
========= Compilation & Run : BEGIN =========

$ g++ --version
g++ (GCC) 3.3.1 (cygming special)
[snip]

$ g++ foo.cpp

$ a

Container<bool>
Container<int>
Container<Foo>
Container<Bar<int>>
Container<Bar<Foo>>

Container<bool>
Container<int>
Container<Foo>
Container<Bar<int>>
Container<Bar<Foo>>

========= Compilation & Run : END ===========
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #3
Alex Vinokur wrote:
"Vincent Cantin" <MY***************@astrocorp.com.tw> wrote in message news:2k************@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

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brian | last post by:
Ok, so i know the title is a little confusing but let me put it this way lets say i have an array of strings, and with each string inside the...
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and...
3
by: Ken Durden | last post by:
Hey, I posted this a few months ago and got completely off-the-chart responses. At the time I wasn't using reflection very much, but more and...
1
by: FD | last post by:
For performance reason, the follwoing code (C#) to capture all the Request collection contexts is NOT recommended: string ID =...
8
by: Ken Sturgeon | last post by:
I have a button inside a panel control. Apparently I can't expect VB to respond to the button's _Click event. How do I capture the click event? ...
2
by: eBob.com | last post by:
I am using regular expressions and a particular feature called "capture" (I think) to suck some information out of some html. I could have never...
1
by: rizwanahmed24 | last post by:
Hello I want to capture the event generated when a control (from toolbox) is added on to the panel at design time. I tried overriding...
6
by: William | last post by:
for example, I have a global object: extern Object myobj; Can gcc get this object by using string "myobj" at runtime?? I know C++ rtti doesnt...
4
by: tahirahmed | last post by:
Hello, Before i start programming my application in C# I want ask a few things about directX capture class. My application scenario I want...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.