Wednesday, 2 October 2013

Template parameter type deduction

Template parameter type deduction

I'm fiddling around with template meta-programming again and trying to get
a better understanding of it. I've created the following trivial template
meta-function.
template<typename T, T Value>
struct is_positive
{
static const bool value = (Value >= 0);
}
const bool is_positive_test_1 = is_positive<int32_t, 3>::value; //true
const bool is_positive_test_2 = is_positive<int32_t, 0>::value; //true
const bool is_positive_test_3 = is_positive<int32_t, -1>::value; //false
Everything works fine, as expected, but I want to know if there is a way
that I can eliminate the need to specify the type of the Value parameter,
so the calling convention would instead be:
is_positive<1>::value
Thank you in advance. :)

No comments:

Post a Comment