Templates, token-pasting, defines and literals...

  • Thread starter Jean-Claude Gervais
  • Start date
J

Jean-Claude Gervais

Hi,

I'm trying to write a template class that knows its own name.

Specifically, I want it to behave this way -

Say I instantiate a template class like this:

my_vector<int> foo;

I'd like to have a method (let's call it GetClassName) in my_vector that
returns a pointer to the string "int".

Or if the instantiation was done with any other class, like CPoint,
calling ClassName() would return "CPoint".

#define paster( n ) ( "class" #n " is the class" )

template <typename T> class my_vector : public std:vector<T> {

public:

static char * GetClassName(void) { return paster( T ); };

};


But I am having problems with doing this in a template. It seems like
the typename T is being taken literally as "T", the string returned is
always

"class T is the class"

Is there a way?
 
P

Panjandrum

Jean-Claude Gervais said:
I'm trying to write a template class that knows its own name.

Specifically, I want it to behave this way -

Say I instantiate a template class like this:

my_vector<int> foo;

I'd like to have a method (let's call it GetClassName) in my_vector that
returns a pointer to the string "int".

Or if the instantiation was done with any other class, like CPoint,
calling ClassName() would return "CPoint".

#define paster( n ) ( "class" #n " is the class" )

template <typename T> class my_vector : public std:vector<T> {

public:

static char * GetClassName(void) { return paster( T ); };

};


But I am having problems with doing this in a template. It seems like
the typename T is being taken literally as "T", the string returned is
always

"class T is the class"

The C preprocessor runs ahead of the C++ template processor. Have you
looked at typeid?
 
V

Victor Bazarov

Jean-Claude Gervais said:
I'm trying to write a template class that knows its own name.

Specifically, I want it to behave this way -

Say I instantiate a template class like this:

my_vector<int> foo;

I'd like to have a method (let's call it GetClassName) in my_vector that
returns a pointer to the string "int".

Or if the instantiation was done with any other class, like CPoint,
calling ClassName() would return "CPoint".

#define paster( n ) ( "class" #n " is the class" )

template <typename T> class my_vector : public std:vector<T> {

public:

static char * GetClassName(void) { return paster( T ); };

};


But I am having problems with doing this in a template. It seems like
the typename T is being taken literally as "T", the string returned is
always

"class T is the class"

Is there a way?

Not really. You could try using 'typeid(T).name()', but there is no
guarantee in the language that it returns anything useful.

V
 
M

Michael Olea

Jean-Claude Gervais said:
Hi,

I'm trying to write a template class that knows its own name.

You might be interested in the thread: typeid(T).nameOfTheBeast()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

templates?? 2
templates 10
Templates and g++ 4
Token pasting problem 1
token pasting help (##) 2
Templates 5
Variadic templates std::tuple 2
Using enable_if and function templates 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top