Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch passed type from template

Is it possible in C++ to check the type passed into a template function? For example:

template <typename T>
void Foo()
{
   if (typeof(SomeClass) == T)
      ...;
   else if (typeof(SomeClass2) == T)
      ...;
}
like image 247
Max Frai Avatar asked Dec 23 '10 20:12

Max Frai


People also ask

Can we pass non type parameters to templates?

Template non-type arguments in C++It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types.

Can template type be a pointer?

A template has only one type, but a specialization is needed for pointer, reference, pointer to member, or function pointer types. The specialization itself is still a template on the type pointed to or referenced.

Why we use switch in C++?

The switch statement in C++ is the best alternative to the lengthy if statements that are used to compare a variable to different integral values. It is a multi-way branch statement. The switch statement is the control statement that allows any value to change the control of the execution.

When C++ reaches a break keyword it breaks out of the switch block this will stop the execution of more code and case testing inside the block?

When C++ reaches a break keyword, it breaks out of the switch block. This will stop the execution of more code and case testing inside the block. When a match is found, and the job is done, it's time for a break. There is no need for more testing.


3 Answers

Yes, it is...but it probably won't work the way you expect.

template < typename T >
void foo()
{
  if (is_same<T,SomeClass>::value) ...;
  else if (is_same<T,SomeClass2>::value) ...;
}

You can get is_same from std:: or boost:: depending on your desire/compiler. The former is only in C++0x.

The problem comes with what is in .... If you expect to be able to make some function call specific to those types within foo, you are sadly mistaken. A compiler error will result even though that section of code is never run when you pass in something that doesn't obey that expected interface.

To solve THAT problem you need to do something a bit different. I'd recommend tag dispatching:

struct v1_tag {};
struct v2_tag {};

template < typename T > struct someclass_version_tag;
template < > struct someclass_version_tag<SomeClass> { typedef v1_tag type; };
template < > struct someclass_version_tag<SomeClass2> { typedef v2_tag type; };

void foo(v1_tag) { ... }
void foo(v2_tag) { ... }
template < typename T > void foo()
{
  typedef typename someclass_version_tag<T>::type tag;
  foo(tag());
}

Note that you will not be suffering any runtime-polymorphism overhead here and with optimizations turned on it should result in the same or even smaller code size AND speed (though you shouldn't be worrying about that anyway until you've run a profiler).

like image 60
Edward Strange Avatar answered Nov 02 '22 18:11

Edward Strange


If you want to do something specific based on the type, specialize the template:

template <typename T>
void Foo() { }

template <>
void Foo<SomeClass>() { }

template <> 
void Foo<SomeClass2>() { }

// etc.

(You don't actually want to specialize the function template, though; this is for exposition only. You'll either want to overload the template if you can, or delegate to a specialized class template. For more on why and how to avoid specializing function templates, read Herb Sutter's Why Not Specialize Function Templates?)

like image 21
James McNellis Avatar answered Nov 02 '22 16:11

James McNellis


No, however you can use partial specialization :

template<typename T>
struct Bar { static void foo(); };
template<typename T>
template<> inline void Bar<T>::foo() {
//generic
}
template<> inline void Bar<int>::foo() {
//stuff for int
}
template<> inline void Bar<QString>::foo() {
//QString
}

Edit Yes with type traits, however it's not really needed. Edit 2 type_traits example.

#include <type_traits>
template<typename T> void foo() {
    using std::is_same;
    if<is_same<T, T2>::value || is_same<T, T1>::value) { 
        /* stuff */
    }
}
like image 4
OneOfOne Avatar answered Nov 02 '22 16:11

OneOfOne