how to determine type-type?

G

Gernot Frisch

er... what I want to know is:

template <class T> void foo(void)
{
switch (typeof(T))
{
}
}

Know what I mean?
 
R

Rolf Magnus

Gernot said:
er... what I want to know is:

template <class T> void foo(void)
{
switch (typeof(T))
{
}
}

Know what I mean?

No. Maybe you're searching template specialization:

template <class T> void foo(T value)
{
// general version
}

template <> void foo(int value)
{
// specialized version for int
}
 
T

Thomas Matthews

Gernot said:
er... what I want to know is:

template <class T> void foo(void)
{
switch (typeof(T))
{
}
}

Know what I mean?

There is no method to use switch for types.

One philosophy says that if need to switch based
on type, something is wrong with your design.

See Andrei A.'s book, "Modern C++ Design" for
"typelists". Also search this newsgroup and
the web.

You may also want to search for "double dispatch"
too.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
U

Ulrich Achleitner

er... what I want to know is:

template <class T> void foo(void)
{
switch (typeof(T))
{
}
}

Know what I mean?


you could partially specialize your function, ie. supply distinct
implementations for specific types:

template <class T> void foo(void)
{
//do what should be done for all types except Type1 and Type2 (see below)
}

template <> void foo<Type1>(void)
{
//do what is necessary for Type1
}

template <> void foo<Type2>(void)
{
//do what is necessary for Type2
}


PS: a switch statement only works with integral constants
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top