template with C++ Pre-processor concatenates ##

P

PengYu.UT

I have the following in template class. I want doit() print the type
info. But it doesn't work. Could you please show me what is the correct
way to do that?

#include <iostream>

#define to_string( s ) # s
template <typename T>
class A {
public:
void doit(){
std::cout << to_string(T) << std::endl;
}
};

int main(int argc, char *argv[])
{
A<int> a;
a.doit();
}
 
G

Greg Comeau

I have the following in template class. I want doit() print the type
info. But it doesn't work. Could you please show me what is the correct
way to do that?

#include <iostream>

#define to_string( s ) # s
template <typename T>
class A {
public:
void doit(){
std::cout << to_string(T) << std::endl;
}
};

int main(int argc, char *argv[])
{
A<int> a;
a.doit();
}

Preprocessing is done before template instantiation,
so to_string(T) has long since been "T"d by then time A<something>
comes into being. There are probably ways to do this but none
except:

typeid(T).name()

leap out to me this second, and that output is implementation defined
as I recall it.
 
P

PengYu.UT

Greg said:
I have the following in template class. I want doit() print the type
info. But it doesn't work. Could you please show me what is the correct
way to do that?

#include <iostream>

#define to_string( s ) # s
template <typename T>
class A {
public:
void doit(){
std::cout << to_string(T) << std::endl;
}
};

int main(int argc, char *argv[])
{
A<int> a;
a.doit();
}

Preprocessing is done before template instantiation,
so to_string(T) has long since been "T"d by then time A<something>
comes into being. There are probably ways to do this but none
except:

typeid(T).name()

leap out to me this second, and that output is implementation defined
as I recall it.

I changed the example program to the following and compiled with
g++-3.3. I couldn't get what I want. The output was:
4temp
i
d

Does g++ compiler follow the standard?
////////////////////
#include <iostream>
#include <typeinfo>

struct temp{
int a;
};
template <typename T>
class A {
public:
void doit(){
std::cout << typeid(T).name() << std::endl;
}
};

int main(int argc, char *argv[])
{
A<temp> a;
a.doit();

A<int> b;
b.doit();

A<double> c;
c.doit();
}
 
J

John Harrison

Greg said:
I have the following in template class. I want doit() print the type
info. But it doesn't work. Could you please show me what is the correct
way to do that?

#include <iostream>

#define to_string( s ) # s
template <typename T>
class A {
public:
void doit(){
std::cout << to_string(T) << std::endl;
}
};

int main(int argc, char *argv[])
{
A<int> a;
a.doit();
}

Preprocessing is done before template instantiation,
so to_string(T) has long since been "T"d by then time A<something>
comes into being. There are probably ways to do this but none
except:

typeid(T).name()

leap out to me this second, and that output is implementation defined
as I recall it.


I changed the example program to the following and compiled with
g++-3.3. I couldn't get what I want. The output was:
4temp
i
d

Does g++ compiler follow the standard?
////////////////////
#include <iostream>
#include <typeinfo>

struct temp{
int a;
};
template <typename T>
class A {
public:
void doit(){
std::cout << typeid(T).name() << std::endl;
}
};

int main(int argc, char *argv[])
{
A<temp> a;
a.doit();

A<int> b;
b.doit();

A<double> c;
c.doit();
}

Yes, because as Greg said, the standard does not specify what the return
value from typeid(T).name() is.

There is no way in standard C++ to do what you want, that I know about.

john
 
G

Greg Comeau

Greg said:
I have the following in template class. I want doit() print the type
info. But it doesn't work. Could you please show me what is the correct
way to do that?

#include <iostream>

#define to_string( s ) # s
template <typename T>
class A {
public:
void doit(){
std::cout << to_string(T) << std::endl;
}
};

int main(int argc, char *argv[])
{
A<int> a;
a.doit();
}

Preprocessing is done before template instantiation,
so to_string(T) has long since been "T"d by then time A<something>
comes into being. There are probably ways to do this but none
except:

typeid(T).name()

leap out to me this second, and that output is implementation defined
as I recall it.

I changed the example program to the following and compiled with
g++-3.3. I couldn't get what I want. The output was:
4temp
i
d

Like I said, the output is implementation defined.
In their case, apparently they use part of the name
mangling forula, so 4temp, i and d mean temp, int and double
respectively.
Does g++ compiler follow the standard?
////////////////////
#include <iostream>
#include <typeinfo>

struct temp{
int a;
};
template <typename T>
class A {
public:
void doit(){
std::cout << typeid(T).name() << std::endl;
}
};

int main(int argc, char *argv[])
{
A<temp> a;
a.doit();

A<int> b;
b.doit();

A<double> c;
c.doit();
}
 
J

Jon Slaughter

I have the following in template class. I want doit() print the type
info. But it doesn't work. Could you please show me what is the correct
way to do that?

#include <iostream>

#define to_string( s ) # s
template <typename T>
class A {
public:
void doit(){
std::cout << to_string(T) << std::endl;
}
};

int main(int argc, char *argv[])
{
A<int> a;
a.doit();
}

You could do something like

#include <iostream>
#include <string>

using namespace std;

template <typename S>
struct StringType
{
static string name;
};

string StringType<int>::name = "int";
string StringType<string>::name = "string";
// etc //

// For some kinda default values
template <typename S>
String StringType<S>::name = typeid(S).name();


template <typename T>
struct A
{
void doit()
{
cout << StringType<T>::name << endl;
}
};

int main(int argc, char *argv[])
{
A<string> a;
a.doit();
}


this has the effect that when one wants to add a new type they get to create
the string for it, ofcourse one would like to have a default string for all
types but I can't see how to do this at the moment(whithout using typeid).
Unfortunately it seems, atleast in VS2k5, doesn't optimize out the
StringType names that are not used... atleast both names here showed up in
the retail's exe.
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top