如果传递给模板的类型是枚举类型,如何检查(不使用Boost或其他非标准库)?
谢谢。检查类型是枚举的方法
1
A
回答
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
我很欣赏你想总的便携性和不使用升压。如果您发现不切实际,您可能更愿意使用简单的ifdef和以下内容: MSDN在c++ is_enum
的谷歌搜索结果的首页上有类似的功能。 在最近的GNU编译器上,尽量使用using std::tr1::is_enum;
即使你不想使用boost,你也可以检查它用于确定的技术。看起来很复杂,可排除所有其他可能性: - /。
相关问题
- 1. 枚举类型检查
- 2. 检查枚举类型是ulong
- 3. 检查什么类型的枚举进入通用方法
- 4. 检查枚举类
- 5. 枚举类型检查编译器?
- 6. 枚举抛出“不是枚举类型”
- 7. 检索枚举int类型
- 8. 枚举类型中的等于方法
- 9. 有什么办法可以检查一个类型是枚举类型吗?
- 10. 检查枚举
- 11. 检查枚举
- 12. 类型枚举
- 13. 子类检查,是运算符还是枚举检查
- 14. Ç枚举(枚举类型定义)的
- 15. Node.js native枚举中的枚举类型
- 16. Ada:如何检查输入是否枚举类型
- 17. 如何检查一个实例是否为枚举类型
- 18. 检索Z3Py中枚举类型的值
- 19. 检查FileAttributes枚举
- 20. 枚举vs强类型枚举
- 21. 如何在Razor中针对枚举类型进行枚举值检查
- 22. 是枚举实例“封闭”在枚举类型的Java?
- 23. 上枚举类型
- 24. HTML5枚举类型
- 25. 为枚举类型
- 26. 枚举或类型
- 27. psycopg2枚举类型
- 28. 具有泛型类型的泛型枚举枚举
- 29. 枚举与功能的方法(组合类/枚举)
- 30. 如何检查Delphi中枚举的IStorage元素的类型?
@Tony我更新了我的问题 – 2010-09-15 19:17:06
其实,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
@Cătălin:“在Borland C++ Builder 5下破解,而对于版本8之前的Metrowerks编译器“不太可能打扰太多人(可怜的老Borland)。 – 2010-09-15 19:23:25