is_enum实现 [英] is_enum implementation
问题描述
我试图实现 std :: is_enum
。这是我的代码到目前为止:
I'm trying to implement std::is_enum
. Here is my code so far:
template<typename T>
struct is_enum {
static bool value;
};
template<typename T>
bool is_enum<T>::value = false;
template<enum E>
struct is_enum {
static bool value;
};
template<enum E>
bool is_enum<E>::value = true;
此代码导致错误。更精确地:
This code causes error. More precisely:
g++ -std=c++0x -Wall -o "enum2" "enum2.cpp" (in directory: /home/aristophanes/Desktop/C++)
Compilation failed.
enum2.cpp:11:15: error: use of enum ‘E’ without previous declaration
enum2.cpp:3:10: error: template parameter ‘class T’
enum2.cpp:12:8: error: redeclared here as ‘int E’
enum2.cpp:16:15: error: use of enum ‘E’ without previous declaration
enum2.cpp:17:14: error: ‘E’ was not declared in this scope
enum2.cpp:17:15: error: template argument 1 is invalid
enum2.cpp:17:18: error: template declaration of ‘bool value’
任何人都可以向我解释错误?这是我的还是编译器的错?提前感谢。
Can anyone explain to me where I make a mistake? It is mine or the compiler's fault? Thanks in advance.
编辑:如果完全错误,那么如何更正?
if it is completely wrong, then how can I correct it?
注意:我使用 g ++ -o< file> < file> .cpp
推荐答案
实现这个的最好方法是使用编译器魔法,
The best way to implement this is to use compiler magic, and I believe most implementations do this.
例如,这里是libc ++的实现gcc> = 4.3和任何编译器 __ has_feature(is_enum)
1
For example, here's libc++'s implementation for gcc >= 4.3 and any compiler that __has_feature(is_enum)
1
template <class _Tp> struct _LIBCPP_VISIBLE is_enum
: public integral_constant<bool, __is_enum(_Tp)> {};
\\ b $ b对于所有其他编译器libc ++ does:
For all other compilers libc++ does:
template <class _Tp> struct _LIBCPP_VISIBLE is_enum
: public integral_constant<bool, !is_void<_Tp>::value &&
!is_integral<_Tp>::value &&
!is_floating_point<_Tp>::value &&
!is_array<_Tp>::value &&
!is_pointer<_Tp>::value &&
!is_reference<_Tp>::value &&
!is_member_pointer<_Tp>::value &&
!is_union<_Tp>::value &&
!is_class<_Tp>::value &&
!is_function<_Tp>::value > {};
一些其他类型的traits仍需要编译器的魔力。 2 is_union
。然而,该条件可以被重写,使得其不需要编译器魔术。这可以通过用对于两者的单个检查替换对联合和类的单独检查来完成,如Johannes Schaub指出的。
Some of those other type traits still require compiler magic.2 E.g. is_union
. However, that condition can be rewritten such that it doesn't need compiler magic. This can be done by replacing the seperate checks for unions and classes with a single check for both, as Johannes Schaub points out.
1。据我所知,只有clang实现 __ has_feature
,不幸的是。
2。很有趣的是,libc ++确实有不使用编译器内在函数的 is_union< T>
和 is_class< T>
,但是结果他们为联合类型提供错误的结果。但是它们的错误结果是互补的,因此libc ++的 is_enum< T>
的后备实现提供了准确的结果。
1. So far as I know only clang implements __has_feature
, unfortunately.
2. It's interesting that libc++ does have a version of is_union<T>
and is_class<T>
that do not use compiler intrinsics, but as a result they provide erroneous results for union types. But their erroneous results are complementary so libc++'s fallback implementation of is_enum<T>
provides accurate results.
这篇关于is_enum实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!