2010-09-15 99 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