473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2549

"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 array, i want to make a new class problems: 1)...
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 __FILE___ macros for getting this, but I cannot find a...
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 more I find myself wanting to use it to write...
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 = Request.QueryString; But how to Capture the individual Request...
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? Thanks Ken
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 come up with this myself but Balena has an example...
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 OnControlAdded method, and capturing ControlAdded event but...
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 support this, but I dont know if gcc can , ...
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 to design an application which is able to detect...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.