Template Specialization for basic POD only
If you really want only fundamental types and not user-defined POD types then the following should work:
#include <iostream>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/is_fundamental.hpp>
#include <boost/type_traits/is_same.hpp>
template<typename T>
struct non_void_fundamental : boost::integral_constant<
bool,
boost::is_fundamental<T>::value && !boost::is_same<T, void>::value
>
{ };
template<typename T, bool Enable = non_void_fundamental<T>::value>
struct DoStuff
{
void operator ()() { std::cout << "Generic\n"; } const
};
template<>
struct DoStuff<T, true>
{
void operator ()() { std::cout << "POD Type\n"; } const
};
If you also want user-defined POD types, then use boost::is_pod<>
instead of non_void_fundamental<>
(and if you're using C++11 and doing this for optimization purposes, use std::is_trivially_copyable<>
instead).
In C++11, many traits have been added to the standard library, and most seem particularly aimed toward interesting specializations (and notably bitwise manipulations).
The top-level trait you could be interested in is std::is_trivial
, however there are many others:
std::is_trivially_default_constructible
std::is_trivially_copy_constructible
std::is_trivially_move_constructible
std::is_trivially_copyable
(can be copied viamemcpy
)
In general, the Standard has tried to get as finer grained traits as possible so you need not rely on such broad assumptions as is_pod
but instead fine-tune your constraints to match what your methods really need.