Suppose I want to write a generic function void f<T>() that does one if T is a POD type and the other thing if T is a non-POD (or any other arbitrary predicate).
One way to achieve this is to use a tag submission template, such as a standard library, with iterator categories:
template <bool> struct podness {}; typedef podness<true> pod_tag; typedef podness<false> non_pod_tag; template <typename T> void f2(T, pod_tag) { } template <typename T> void f2(T, non_pod_tag) { } template <typename T> void f(T x) {
An alternative would be to use a static member function of partially specialized types:
template <typename T, bool> struct f2; template <typename T> struct f2<T, true> { static void f(T) { } }; template <typename T> struct f2<T, false> { static void f(T) { } }; template <typename T> void f(T x) {
What are the pros and cons of using one method over another? What would you recommend?
c ++ generic-programming metaprogramming partial-specialization
Peter Alexander Aug 02 '11 at 18:53 2011-08-02 18:53
source share