Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::is_enum how it works?

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.

like image 517
Torsten Avatar asked Oct 26 '12 07:10

Torsten


2 Answers

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 returns true if T is an enum and false otherwise. MSDN has a list of such intrinsics that Visual C++ implements for type trait support.

is_enum is now part of C++11, and is included in the type_traits header. 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 for BOOST_IS_ENUM in 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).

like image 160
Praetorian Avatar answered Nov 07 '22 11:11

Praetorian


boost::is_enum is implemented like std::is_enum. It requires some compiler magic. Check the following link which has the same question, and an implementation: is_enum implementation

like image 40
Rontogiannis Aristofanis Avatar answered Nov 07 '22 12:11

Rontogiannis Aristofanis