While integral_constant
is the answer, you may make a type or two just to make things a bit clearer. I use this in my library:
// utility/bool_type.hpp
#include <boost/type_traits/integral_constant.hpp>
namespace utility
{
template <bool B>
struct bool_type : boost::integral_constant<bool, B>
{
static const bool value = B;
};
typedef const boost::true_type& true_tag;
typedef const boost::false_type& false_tag;
}
// main.cpp
// just some predicate for example
template <typename T>
struct is_pointer
{
static const bool value = false;
};
template <typename T>
struct is_pointer<T*>
{
static const bool value = true;
};
// some specialized function.
// the true/false tag parameter is a
// little easier to read, i think
template <typename T>
void foo(T, utility::true_tag)
{
}
/* versus:
template <typename T>
void foo(T, const boost::true_type&)
*/
template <typename T>
void foo(T, utility::false_tag)
{
}
// the actual function
template <typename T>
void foo(T pX)
{
// a bit shorter
foo(pX, utility::bool_type<is_pointer<T>::value>());
/* versus:
foo(pX, boost::integral_type<bool, is_pointer<T>::value>());
*/
}
int main(void)
{
int i = 0;
foo(i);
foo(&i);
}
But that's just my opinion. :]