假设我们有一个整数值的包装器。例如,一个布尔包装器像 std::true_type
和 std::false_type
:
template<typename T , T VALUE>
struct integral_value_wrapper
{
static const T value = VALUE;
};
template<bool VALUE>
using boolean_wrapper = integral_value_wrapper<bool,VALUE>;
using true_wrapper = boolean_wrapper<true>;
using false_wrapper = boolean_wrapper<false>;
我们在自己的类中使用布尔封装器。例如,一个整数检查器:
template<typename T>
struct is_int : public false_wrapper {};
template<>
struct is_int<int> : public true_wrapper {};
using type = int;
int main()
{
if( is_int<type>::value ) cout << "type is int" << endl;
}
我的问题是:有没有办法使类型(在本例中继承自bool包装器的类)隐式转换为整数值?
这可以避免在布尔表达式中使用::value
成员,比如下面的例子:
using type = int;
int main()
{
if( is_int<type> ) cout << "type is int" << endl; //How I can do that?
}