STD :: IS_POLYORMOLIC如何识别多态性?
我试图了解std :: is_polymorphc
在C ++中的工作。 这是在type_traits.h
中定义的:
template <class _Ty>
struct is_polymorphic : bool_constant<__is_polymorphic(_Ty)> {}; // determine whether _Ty is a polymorphic type
template <class _Ty>
_INLINE_VAR constexpr bool is_polymorphic_v = __is_polymorphic(_Ty);
我无法找到__ IS_POLYMORPHIC
的源代码。 有人可以帮助我了解__ is_polymorphic
有效吗?
I tried to understand the working of std::is_polymorphc
in C++.
This is defined in type_traits.h
:
template <class _Ty>
struct is_polymorphic : bool_constant<__is_polymorphic(_Ty)> {}; // determine whether _Ty is a polymorphic type
template <class _Ty>
_INLINE_VAR constexpr bool is_polymorphic_v = __is_polymorphic(_Ty);
I am not able to find the source code for __is_polymorphic
.
Could someone help me understand how __is_polymorphic
works ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
__ is_polymorphic
是保留的关键字,因此它内置的编译器IE IE,它不是在库中实现的,它直接在编译器中实现。因此,除非您查看编译器的源代码,否则没有源代码可以查看。on cppreference ,您可以看到一个可能的实现:
这是通过使用
dynamic_cast
需要一种多态类型才能进行编译的事实来起作用的。detect_is_polymorphic
是一个使用SFINAE的重载功能,检查dynamic_cast
是否有效在t
上。__is_polymorphic
is a reserved keyword, so it's built-in to the compiler i.e. it's not implemented in library, it's implemented directly in the compiler. So, there is no source code to see, unless you look at the compiler's source code.On cppreference, you can see a possible implementation:
This works by using the fact that
dynamic_cast
requires a polymorphic type in order to compile.detect_is_polymorphic
is an overloaded function that uses SFINAE to check ifdynamic_cast
is valid onT
.有魔术。我知道,这是不满意的,但这就是答案。大多数
type_traits
是编译器级魔术。 C ++编译器知道,当它看到is_polymorphic
之类的东西时,它应该浏览参数类可用的方法列表(这不是您可以作为C ++程序员做的事情,它只是编译器的内容可以做)并通过临时方法进行检查。With magic. I know, it's unsatisfying, but that's the answer. Most of
type_traits
is compiler-level magic. The C++ compiler knows that when it sees something likeis_polymorphic
, it should look through the list of methods available to the argument class (which is not something you can do as a C++ programmer, it's something only the compiler can do) and does the check via an ad-hoc method.