STD :: IS_POLYORMOLIC如何识别多态性?

发布于 2025-02-07 08:14:13 字数 457 浏览 1 评论 0原文

我试图了解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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人疚 2025-02-14 08:14:13

__ is_polymorphic是保留的关键字,因此它内置的编译器IE IE,它不是在库中实现的,它直接在编译器中实现。因此,除非您查看编译器的源代码,否则没有源代码可以查看。

on cppreference ,您可以看到一个可能的实现:

 命名空间详细信息{

t&gt; lt; lt; lt;
std :: true_type detect_is_polymorphic(
    DECLTYPE(Dynamic_cast&lt; const volatile void*&gt;(static_cast&lt; t*&gt;(nullptr)))))
);
t&gt; lt; lt; lt;
std :: false_type detect_is_polymorphic(...);

} //名称空间细节

t&gt; lt; lt; lt;
struct is_polymorphic:declType(lides :: detct_is_polymorphic&lt; t&gt;(nullptr)){}};
 

这是通过使用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:

namespace detail {

template <class T>
std::true_type detect_is_polymorphic(
    decltype(dynamic_cast<const volatile void*>(static_cast<T*>(nullptr)))
);
template <class T>
std::false_type detect_is_polymorphic(...);

} // namespace detail

template <class T>
struct is_polymorphic : decltype(detail::detect_is_polymorphic<T>(nullptr)) {};

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 if dynamic_cast is valid on T.

娇柔作态 2025-02-14 08:14:13

有魔术。我知道,这是不满意的,但这就是答案。大多数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 like is_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文