#include <stdio.h>
#include <type_traits>
void print()
{
printf("cheers from print !\n");
}
class A
{
public:
void print()
{
printf("cheers from A !");
}
};
template<typename Function>
typename std::enable_if< std::is_function<
typename std::remove_pointer<Function>::type >::value,
void >::type
run(Function f)
{
f();
}
template<typename T>
typename std::enable_if< !std::is_function<
typename std::remove_pointer<T>::type >::value,
void >::type
run(T& t)
{
t.print();
}
int main()
{
run(print);
A a;
run(a);
return 0;
}
The code above compiles and print as expected:
cheers from print ! cheers from A !
what I would like to express is : "if the template is function then apply this function, else ...". Or in another formulation : having a version of the function for function templates, and a default version for non function templates.
so, this part seems somehow redundant, and could be "replaced" by a "else" condition :
template<typename T>
typename std::enable_if< !std::is_function<
typename std::remove_pointer<T>::type >::value,
void >::type
run(T& t)
would this exists ?
using
statements to reduce verbosity. – Lasky