小编Not*_*ik.的帖子

std::is_union 实现如何工作?

我目前正在更详细地了解 C++ 标准库,我想知道它的实现是如何std::is_union工作的。在 libcxx (LLVM) 中,除了直接使用可能内置的 之外__is_union,它还定义为

template <class _Tp> struct __libcpp_union : public false_type {};
template <class _Tp> struct _LIBCPP_TEMPLATE_VIS is_union
    : public __libcpp_union<typename remove_cv<_Tp>::type> {};
Run Code Online (Sandbox Code Playgroud)

同样,STLPort 虽然很老,但它的实现更加简约:

template <class T>
struct is_union
{ };
Run Code Online (Sandbox Code Playgroud)

这似乎总是解析为std::false_type一个空结构,或者更糟糕的是,一个空结构,但事实并非如此;这是如何实现的?在另一个问题中,一个答案指出,如果is_union没有编译器钩子就无法实现,但这是否意味着 libcxx、STLPort 以及可能所有主要实现都不能移植到任何不能自动使其工作的编译器?

c++ language-implementation

1
推荐指数
1
解决办法
619
查看次数

标签 统计

c++ ×1

language-implementation ×1