I hope it's self-explaining
struct TrueType {};
struct FalseType {};
template<class T>
struct IsConst
{
typedef FalseType Result;
};
template<class T>
struct IsConst<const T>
{
typedef TrueType Result;
};
//now, const/non-const specific code
template<class T>
void func(T t)
{
//dispatching call to the appropriate implementation
doFunc(t, (IsConst<T>::Result*)NULL);
}
template<class T>
void doFunc(T t, TrueType*)
{
std::cout << "func implementation for constants";
}
template<class T>
void doFunc(T t, FalseType*)
{
std::cout << "func implementation for not constants";
}
//usage
int a = 6;
const int b = 5;
func(a);
func(b);
You also need a specialization of IsConst template for const pointers, though.
However it's not working for calls with values (like "func(5);")