473,770 Members | 2,096 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
14 2211
In article <ZL************ ********@speake asy.net>,
Gianni Mariani <gi*******@mari ani.ws> wrote:
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 ?


Using std::tr1::type_ traits (or boost::type_tra its) it is relatively
easy to perform arbitrarily complex compile-time tests and dispatch (at
compile time) based on the test results:

template <typename T>
class foo {
public:
foo()
{
foo_imp(std::tr 1::integral_con stant<bool, test<T>::value> ());
}

private:
template <class U>
struct test
{
private:
typedef typename std::tr1::remov e_cv<U>::type cv_removed;
typedef typename
std::tr1::remov e_reference<U>: :type reference_remov ed;
static const bool is_int =
std::tr1::is_sa me<cv_removed, int>::value;
static const bool is_uint =
std::tr1::is_sa me<U, unsigned>::valu e;
static const bool is_int_ref =
std::tr1::is_sa me<reference_re moved, int>::value;
public:
static const bool value = is_int || is_uint || is_int_ref;
};

void foo_imp(std::tr 1::true_type)
{ cerr << "int\n"; }
void foo_imp(std::tr 1::false_type)
{ }
};

-Howard
Jan 1 '06 #11
Howard Hinnant wrote:
....

Using std::tr1::type_ traits (or boost::type_tra its) it is relatively
easy to perform arbitrarily complex compile-time tests and dispatch (at
compile time) based on the test results:


Sure.

The point I was trying to make is that it may be insufficient to test
for int alone.
Jan 1 '06 #12
In article <11************ **********@f14g 2000cwb.googleg roups.com>,
"Joseph Turian" <tu****@gmail.c om> wrote:
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?


I believe the order of definition is irrelevant.
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?


As someone else mentioned, you can sub in boost::type_tra its if you
don't have tr1 yet (www.boost.org).

Here is the latest documentation for tr1:

http://www.open-std.org/jtc1/sc22/wg...2005/n1836.pdf

And here's a link to the freely available boost library which inspired
this part of tr1:

http://www.boost.org/doc/html/boost_typetraits.html

is_same<T, U> will work for arbitrary types. It is both clever and
simple:

template <class T, class U> struct is_same
: public integral_consta nt<bool, false> {};
template <class T> struct is_same<T, T>
: public integral_consta nt<bool, true> {};

where integral_consta nt is just a helper class:

template <class T, T v>
struct integral_consta nt
{
static const T value = v;
typedef T value_type;
typedef integral_consta nt<T, v> type;
};

typedef integral_consta nt<bool, true> true_type;
typedef integral_consta nt<bool, false> false_type;

Be aware that const T and T are two different types according to
is_same. But the same type_traits package can strip cv-qualifiers.

The type_traits lib is invaluable for making compile-time
decisions/computations on template parameters. If it helps, here's a
jpeg representation of the type hierarchy which type_traits implements.

http://home.twcny.rr.com/hinnant/cpp...eHiearchy.jpeg

This also roughly follows the classification laid out in section 3.9 of
the standard (modulo those parts the jpeg marks as "proposed" which are
also absent in the boost and tr1 libs).

-Howard
Jan 1 '06 #13
which item? thanks

Jan 4 '06 #14
baibaichen wrote:
which item? thanks


Somewhere in http://www.parashift.com/c++-faq-lite/
Jan 4 '06 #15

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

Similar topics

0
1101
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
1880
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é Schäfer | 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
7818
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
9618
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
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
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
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 project—planning, coding, testing, and deployment—without 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...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
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();...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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.