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

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 2183
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**********************@z14g2000cwz.googlegroups .com>,
"Joseph Turian" <tu****@gmail.com> 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::tr1::is_same<T, int>());
}
private:
void foo_imp(std::tr1::true_type)
{ cerr << "int\n"; }
void foo_imp(std::tr1::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::tr1::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 characterization. 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 characterization. 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::tr1::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::tr1::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
In article <ZL********************@speakeasy.net>,
Gianni Mariani <gi*******@mariani.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_traits) 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::tr1::integral_constant<bool, test<T>::value>());
}

private:
template <class U>
struct test
{
private:
typedef typename std::tr1::remove_cv<U>::type cv_removed;
typedef typename
std::tr1::remove_reference<U>::type reference_removed;
static const bool is_int =
std::tr1::is_same<cv_removed, int>::value;
static const bool is_uint =
std::tr1::is_same<U, unsigned>::value;
static const bool is_int_ref =
std::tr1::is_same<reference_removed, int>::value;
public:
static const bool value = is_int || is_uint || is_int_ref;
};

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

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

Using std::tr1::type_traits (or boost::type_traits) 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**********************@f14g2000cwb.googlegroups .com>,
"Joseph Turian" <tu****@gmail.com> 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::tr1::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_traits 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_constant<bool, false> {};
template <class T> struct is_same<T, T>
: public integral_constant<bool, true> {};

where integral_constant is just a helper class:

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

typedef integral_constant<bool, true> true_type;
typedef integral_constant<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
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...
2
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...
11
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
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...
5
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...
2
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...
4
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...
7
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}"...
2
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.