473,385 Members | 1,693 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,385 software developers and data experts.

template with C++ Pre-processor concatenates ##

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();
}

Oct 15 '05 #1
5 3036
In article <11**********************@g47g2000cwa.googlegroups .com>,
Pe*******@gmail.com <Pe*******@gmail.com> wrote:
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.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 15 '05 #2

Greg Comeau wrote:
In article <11**********************@g47g2000cwa.googlegroups .com>,
Pe*******@gmail.com <Pe*******@gmail.com> wrote:
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();
}

Oct 15 '05 #3
Pe*******@gmail.com wrote:
Greg Comeau wrote:
In article <11**********************@g47g2000cwa.googlegroups .com>,
Pe*******@gmail.com <Pe*******@gmail.com> wrote:
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
Oct 15 '05 #4
In article <11********************@f14g2000cwb.googlegroups.c om>,
Pe*******@gmail.com <Pe*******@gmail.com> wrote:
Greg Comeau wrote:
In article <11**********************@g47g2000cwa.googlegroups .com>,
Pe*******@gmail.com <Pe*******@gmail.com> wrote:
>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();
}

--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 15 '05 #5

<Pe*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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.
Oct 16 '05 #6

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

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
10
by: william xuuu | last post by:
Actually, I also got linker errors with template functions and template classes. And I avoided both of them successfully, by pouring foo.cpp into foo.h, according to the C++ FAQ. ...
0
by: Robbie Hatley | last post by:
I'd always thougth that a C++ compiler/linker should be able to instantiate a template in mulitple places (say, in two different translation units), even using the same template parameters so that...
8
by: mast2as | last post by:
Hi guys, I think from what I found on the net that the code is correct and there's no problem when I compile the files. The problems occurs when I link them. I have a friend function which outputs...
14
by: mens libertina | last post by:
Disclaimer: In addition to reading the skimpy entries at php.net, I've searched this group and alt.php to no avail. Basically, I'm trying to use a template to send email reminders, but I can't...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
14
by: Jess | last post by:
Hello, I was told that if I have a template class or template function, then the definitions must be put into the header file where I put the declarations. On the other hand, it is generally...
6
by: Jon | last post by:
Normally I can search and find answers to things like this but with this one I'm not even sure what to search for and haven't had any luck. Anyway, I'm trying to use a template name as a template...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.