I’ve interesting how this thing is work 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 try to explore source code but it was too hard (macros + templates code navigation fails). Someone can get theory exploration how it works? I’ve have no ideas how it can be implemented.
You’re running into a lot of macros because Boost is switching between compiler intrinsics for all the platforms it supports. For instance, Visual C++ defines
__is_enum(T)which will returnstrueifTis anenumandfalseotherwise. MSDN has a list of such intrinsics that Visual C++ implements for type trait support.is_enumis now part of C++11, and is included in thetype_traitsheader. Looking through your standard library implementation will most likely be easier than the Boost headers.EDIT:
I found the Boost implementation; it is located in
<boost_path>\boost\type_traits\intrinsics.hpp. Search this file forBOOST_IS_ENUMin this file and you’ll see the compiler intrinsic implemented by various compilers. Interestingly enough, it seems all of them implement this particular one as__is_enum(T).