Connecting Tech Pros Worldwide Help | Site Map

Determine type of typename T?

Joseph Turian
Guest
 
Posts: n/a
#1: Dec 31 '05

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

Mateusz Łoskot
Guest
 
Posts: n/a
#2: Dec 31 '05

re: Determine type of typename T?


Joseph Turian wrote:[color=blue]
> How can I determine the type of some particular typename?
>[/color]

typeid(T)

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Howard Hinnant
Guest
 
Posts: n/a
#3: Dec 31 '05

re: Determine type of typename T?


In article <1136060578.455159.126860@z14g2000cwz.googlegroups .com>,
"Joseph Turian" <turian@gmail.com> wrote:
[color=blue]
> 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";
> }
> };[/color]

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
Joseph Turian
Guest
 
Posts: n/a
#4: Dec 31 '05

re: Determine type of typename T?


Mateusz Loskot wrote:[color=blue][color=green]
> > How can I determine the type of some particular typename?[/color]
> typeid(T)[/color]

I'll see if I can figure out the syntax to use 'typeid'
[color=blue]
> template <typename T>
> foo<T>::foo()
> {
> }
>
> template <>
> foo<int>::foo()
> {
> cerr << "int\n";
> }[/color]

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?
[color=blue]
> foo_imp(std::tr1::is_same<T, int>());[/color]

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

puzzlecracker
Guest
 
Posts: n/a
#5: Dec 31 '05

re: Determine type of typename T?


[color=blue]
> 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[/color]


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

Luke Meyers
Guest
 
Posts: n/a
#6: Dec 31 '05

re: Determine type of typename T?


puzzlecracker wrote:[color=blue]
> it is not supported by most compilers and won't be until next
> standardization[/color]

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

puzzlecracker
Guest
 
Posts: n/a
#7: Dec 31 '05

re: Determine type of typename T?



Luke Meyers wrote:[color=blue]
> puzzlecracker wrote:[color=green]
> > it is not supported by most compilers and won't be until next
> > standardization[/color]
>
> 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[/color]

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

Victor Bazarov
Guest
 
Posts: n/a
#8: Dec 31 '05

re: Determine type of typename T?


Joseph Turian wrote:[color=blue]
> 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![/color]

This is covered in the FAQ.


benben
Guest
 
Posts: n/a
#9: Jan 1 '06

re: Determine type of typename T?


>[color=blue]
> I was refering to foo_imp(std::tr1::is_same<T, int>());....
>[/color]

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
Gianni Mariani
Guest
 
Posts: n/a
#10: Jan 1 '06

re: Determine type of typename T?


Joseph Turian wrote:[color=blue]
> 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";
> }
> };[/color]

What about different types of int ?

const int
unsigned int
volatile int
int &

or any combination of those ?
Howard Hinnant
Guest
 
Posts: n/a
#11: Jan 1 '06

re: Determine type of typename T?


In article <ZL-dnfOSi7BmmCXeRVn-rg@speakeasy.net>,
Gianni Mariani <gi2nospam@mariani.ws> wrote:
[color=blue]
> Joseph Turian wrote:[color=green]
> > 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";
> > }
> > };[/color]
>
> What about different types of int ?
>
> const int
> unsigned int
> volatile int
> int &
>
> or any combination of those ?[/color]

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
Gianni Mariani
Guest
 
Posts: n/a
#12: Jan 1 '06

re: Determine type of typename T?


Howard Hinnant wrote:
....[color=blue]
>
> 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:[/color]

Sure.

The point I was trying to make is that it may be insufficient to test
for int alone.
Howard Hinnant
Guest
 
Posts: n/a
#13: Jan 1 '06

re: Determine type of typename T?


In article <1136065266.523197.164230@f14g2000cwb.googlegroups .com>,
"Joseph Turian" <turian@gmail.com> wrote:
[color=blue][color=green]
> > template <typename T>
> > foo<T>::foo()
> > {
> > }
> >
> > template <>
> > foo<int>::foo()
> > {
> > cerr << "int\n";
> > }[/color]
>
> 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?[/color]

I believe the order of definition is irrelevant.
[color=blue]
>[color=green]
> > foo_imp(std::tr1::is_same<T, int>());[/color]
>
> 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?[/color]

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
baibaichen
Guest
 
Posts: n/a
#14: Jan 4 '06

re: Determine type of typename T?


which item? thanks

Victor Bazarov
Guest
 
Posts: n/a
#15: Jan 4 '06

re: Determine type of typename T?


baibaichen wrote:[color=blue]
> which item? thanks
>[/color]

Somewhere in http://www.parashift.com/c++-faq-lite/
Closed Thread


Similar C / C++ bytes