Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 31st, 2005, 08:35 PM
Joseph Turian
Guest
 
Posts: n/a
Default 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

  #2  
Old December 31st, 2005, 08:45 PM
Mateusz Łoskot
Guest
 
Posts: n/a
Default 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
  #3  
Old December 31st, 2005, 09:35 PM
Howard Hinnant
Guest
 
Posts: n/a
Default 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
  #4  
Old December 31st, 2005, 09:55 PM
Joseph Turian
Guest
 
Posts: n/a
Default 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

  #5  
Old December 31st, 2005, 10:15 PM
puzzlecracker
Guest
 
Posts: n/a
Default 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

  #6  
Old December 31st, 2005, 10:45 PM
Luke Meyers
Guest
 
Posts: n/a
Default 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

  #7  
Old December 31st, 2005, 10:55 PM
puzzlecracker
Guest
 
Posts: n/a
Default 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>());....

  #8  
Old December 31st, 2005, 10:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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.


  #9  
Old January 1st, 2006, 02:25 AM
benben
Guest
 
Posts: n/a
Default 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
  #10  
Old January 1st, 2006, 04:55 PM
Gianni Mariani
Guest
 
Posts: n/a
Default 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 ?
  #11  
Old January 1st, 2006, 05:45 PM
Howard Hinnant
Guest
 
Posts: n/a
Default 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
  #12  
Old January 1st, 2006, 06:05 PM
Gianni Mariani
Guest
 
Posts: n/a
Default 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.
  #13  
Old January 1st, 2006, 07:05 PM
Howard Hinnant
Guest
 
Posts: n/a
Default 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
  #14  
Old January 4th, 2006, 06:05 AM
baibaichen
Guest
 
Posts: n/a
Default Re: Determine type of typename T?

which item? thanks

  #15  
Old January 4th, 2006, 02:35 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Determine type of typename T?

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

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

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles