boost :: is_enum是如何工作的?

Tor*_*ten 7 c++ boost type-traits

我很有意思这件事是如何在理论上起作用的.例:

#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;
}
Run Code Online (Sandbox Code Playgroud)

我尝试探索源代码,但它太难了(宏+模板代码导航失败).有人可以通过理论探索它是如何工作的吗?我不知道如何实施它.

Pra*_*ian 7

您遇到了很多宏,因为Boost正在为它支持的所有平台在编译器内在函数之间切换.例如,Visual C++定义__is_enum(T)哪个将返回,true如果Tenumfalse否则.MSDN有一个Visual C++为类型特征支持实现的内在函数列表.

is_enum现在是C++ 11的一部分,并包含在type_traits标题中.查看标准库实现很可能比Boost标头更容易.

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).