2010-09-15 204 views

回答

2

看着http://www.boost.org/doc/libs/1_44_0/boost/type_traits/is_enum.hpp

如果返回true

::boost::type_traits::ice_or< 
      ::boost::is_arithmetic<T>::value 
     , ::boost::is_reference<T>::value 
     , ::boost::is_function<T>::value 
     , is_class_or_union<T>::value 
     , is_array<T>::value 
     >::value 

然后在此基础模板选择:

// Don't evaluate convertibility to int_convertible unless the type 
// is non-arithmetic. This suppresses warnings with GCC. 
template <bool is_typename_arithmetic_or_reference = true> 
struct is_enum_helper 
{ 
    template <typename T> struct type 
    { 
     BOOST_STATIC_CONSTANT(bool, value = false); 
    }; 
}; 

否则,检查它是否转换为int

template <> 
struct is_enum_helper<false> 
{ 
    template <typename T> struct type 
     : ::boost::is_convertible<typename boost::add_reference<T>::type,::boost::detail::int_convertible> 
    { 
    }; 
}; 

如果你想和Boost一样做,你必须定义所有这些特性。 <type_traits>就是这样。

2

http://www.boost.org/doc/libs/1_44_0/libs/type_traits/doc/html/boost_typetraits/reference/is_enum.html

我很欣赏你想总的便携性和不使用升压。如果您发现不切实际,您可能更愿意使用简单的ifdef和以下内容: MSDN在c++ is_enum的谷歌搜索结果的首页上有类似的功能。 在最近的GNU编译器上,尽量使用using std::tr1::is_enum;

即使你不想使用boost,你也可以检查它用于确定的技术。看起来很复杂,可排除所有其他可能性: - /。

+0

@Tony我更新了我的问题 – 2010-09-15 19:17:06

+0

其实,is_enum应该小心使用。一些编译器不正确地支持它。查看最新版本的boost:http://www.boost.org/doc/libs/1_44_0/libs/type_traits/doc/html/boost_typetraits/reference/is_enum.html – 2010-09-15 19:18:19

+1

@Cătălin:“在Borland C++ Builder 5下破解,而对于版本8之前的Metrowerks编译器“不太可能打扰太多人(可怜的老Borland)。 – 2010-09-15 19:23:25