473,756 Members | 7,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine type of typename T?


How can I determine the type of some particular typename?

I am writing a template, and it needs special case handling for some
particular types:

template <typename T>
class foo {
public:
foo() {
if (T == int) cerr << "int\n";
}
};

Thanks!

Joseph

Dec 31 '05 #1
14 2207
Joseph Turian wrote:
How can I determine the type of some particular typename?


typeid(T)

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 31 '05 #2
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
"Joseph Turian" <tu****@gmail.c om> wrote:
How can I determine the type of some particular typename?

I am writing a template, and it needs special case handling for some
particular types:

template <typename T>
class foo {
public:
foo() {
if (T == int) cerr << "int\n";
}
};


Here's one possibility:

template <typename T>
class foo {
public:
foo() {
}
};

template <>
class foo<int> {
public:
foo() {
cerr << "int\n";
}
};

Here's another:

template <typename T>
class foo {
public:
foo();
};

template <typename T>
foo<T>::foo()
{
}

template <>
foo<int>::foo()
{
cerr << "int\n";
}

And here's another:

#include <tr1/type_traits>
#include <iostream>
using std::cerr;

template <typename T>
class foo {
public:
foo()
{
foo_imp(std::tr 1::is_same<T, int>());
}
private:
void foo_imp(std::tr 1::true_type)
{ cerr << "int\n"; }
void foo_imp(std::tr 1::false_type)
{ }
};

-Howard
Dec 31 '05 #3
Mateusz Loskot wrote:
How can I determine the type of some particular typename? typeid(T)


I'll see if I can figure out the syntax to use 'typeid'
template <typename T>
foo<T>::foo()
{
}

template <>
foo<int>::foo()
{
cerr << "int\n";
}
So foo<T>::foo is used unless it is overloaded (by foo<int>::foo, in
this circumstance).
If I switched the order in which the constructors were declared, would
foo<T> always be used, or would it be identical?
foo_imp(std::tr 1::is_same<T, int>());


This is weird, I'll have to google tr1.
Does it only work for primitive types, or will it also work for
user-defined classes?

Thanks

Joseph

Dec 31 '05 #4
This is weird, I'll have to google tr1.
Does it only work for primitive types, or will it also work for
user-defined classes?

Thanks

Joseph

it is not supported by most compilers and won't be until next
standardization
n

Dec 31 '05 #5
puzzlecracker wrote:
it is not supported by most compilers and won't be until next
standardization


I think that's a slightly pessimistic characterizatio n. gcc, for one,
handles partial template specialization pretty well. I don't know the
status of other compilers (I use gcc, why not?), but given the rise in
popularity and practice of things such as template metaprogramming , I
think it's safe to say that an appreciable number of people have found
compilers that to the job for them just fine.

Luke

Dec 31 '05 #6

Luke Meyers wrote:
puzzlecracker wrote:
it is not supported by most compilers and won't be until next
standardization


I think that's a slightly pessimistic characterizatio n. gcc, for one,
handles partial template specialization pretty well. I don't know the
status of other compilers (I use gcc, why not?), but given the rise in
popularity and practice of things such as template metaprogramming , I
think it's safe to say that an appreciable number of people have found
compilers that to the job for them just fine.

Luke


I was refering to foo_imp(std::tr 1::is_same<T, int>());....

Dec 31 '05 #7
Joseph Turian wrote:
How can I determine the type of some particular typename?

I am writing a template, and it needs special case handling for some
particular types:

template <typename T>
class foo {
public:
foo() {
if (T == int) cerr << "int\n";
}
};

Thanks!


This is covered in the FAQ.
Dec 31 '05 #8
>
I was refering to foo_imp(std::tr 1::is_same<T, int>());....


Why wait for the standardization ?

Things like tr1::is_same are extremely easy to write so just take a
minute to write some of them.

Or just use the boost library.

Ben
Jan 1 '06 #9
Joseph Turian wrote:
How can I determine the type of some particular typename?

I am writing a template, and it needs special case handling for some
particular types:

template <typename T>
class foo {
public:
foo() {
if (T == int) cerr << "int\n";
}
};


What about different types of int ?

const int
unsigned int
volatile int
int &

or any combination of those ?
Jan 1 '06 #10

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

Similar topics

0
1100
by: Jason Ferree | last post by:
I am trying to process all folders in a "sent items" folder in outlook 2000. Most items are of "mail Item" type, but some are of an appointment item type. I've looked through the newsgroups, and I've also looked through Slipstick, and Sue's book. Most of that is geared towards VB 6 it seems. Here is the code snippet Sent_Folder = objNS.GetDefaultFolder Outlook.OlDefaultFolders.olFolderSentMail)
2
2086
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with something like below: class PMinimum { public: template <typename T> bool operator()(const T & a, const T & b)
11
1877
by: Johan | last post by:
Hi Can somebody explain to me why I get this warning message and how I can solve this warning message. Thanks a lot Johan In member function `void
1
1465
by: icedac | last post by:
I have some questions about template and c++ itself. ///////////////////////////////////////////////////////////////////////// q1) see follow c++ code, and compile. it's works only IntelC++8.1 but VC71. Do you know why? Which compiler's activity is C++STANDARD? And I wanna be feed back some explain. :)
5
1527
by: Axter | last post by:
I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem. One thing I don't like about the way the current policy template is setup is that for the ptr_policy class the first template type is different from the template type given to the policy. And on the other policy classes, the template type is the same....
2
2346
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last line is very long. I'm wondering if there is any way to suppress it. I can only think of typedef. But I'm not sure whether I can use typedef for the return type. Would you please help me? Please don't be daunted by the length of the code.
4
3641
by: Frank-Ren Schfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple inheritance (avoiding prosperation of virtual func tables). -- At the same time, a class taking N types shall contain a virtual member function that calls a function according to the number of arguments That means, something like:
7
7817
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}" As far as I know yet -- hence this question -- there is no 'one solution fits all', but instead there are several parts that have to be put together to check. What I have so far is, and would like as much feedback as possible to ensure I've...
2
1537
by: Kibiz0r | last post by:
Basically, I want a templated method to do one thing if the type T has a constructor that takes a std::istringstream, and another thing if it doesn't. The problem is that if I put the T(std::istringstream) call in there, every type that is used for that method has to have that constructor or it errors. Can someone please point me in the right direction?
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9873
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.