Boost :: is_enum, how does it work?

I am wondering how this thing works on theory. Example:

#include <boost/type_traits/is_enum.hpp> #include <iostream> enum foo { AAA, BBB }; typedef foo bar; struct sfoo { enum bar { CCC }; }; int main() { std::cout << boost::is_enum<foo>::value << "\n"; // 1 std::cout << boost::is_enum<bar>::value << "\n"; // 1 std::cout << boost::is_enum<sfoo>::value << "\n"; // 0 std::cout << boost::is_enum<int>::value << "\n"; // 0 std::cout << boost::is_enum<sfoo::bar>::value << "\n"; // 1 return 0; } 

I am trying to learn the source code, but it was too complicated (macro + code navigation templates fail). Can someone get a theoretical study on how this works? I have no idea how this can be implemented.

+7
source share
3 answers

You have a lot of macros because Boost switches between the internal functions of the compiler for all the platforms it supports. For example, Visual C ++ defines __is_enum(T) , which returns true if T is enum and false otherwise. MSDN has a list of such built-in functions that Visual C ++ implements to support type properties.

is_enum now part of C ++ 11 and is included in the type_traits header. Looking through the standard library implementation is likely to be easier than the Boost headers.

EDIT:
I found a Boost implementation; it is located in <boost_path>\boost\type_traits\intrinsics.hpp . Find this file for BOOST_IS_ENUM in this file, and you will see the built-in compiler implemented by various compilers. Interestingly, they all implement this particular one as __is_enum(T) .

+5
source

boost::is_enum is implemented as std::is_enum . This requires some compiler magic. Check out the following link that has the same question and implementation: is_enum implementation

+2
source

I did not closely follow the Boost code, but it seems to use a simple exception: an enumeration is something that is not arithmetic (built-in integers and floating point types and pointers), not a link, not a function, not a class or a union, not an array.

+1
source

All Articles