Namespaces
Variants

std:: is_polymorphic

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
is_polymorphic
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
Definiert im Header <type_traits>
template < class T >
struct is_polymorphic ;
(seit C++11)

std::is_polymorphic ist ein UnaryTypeTrait .

Wenn T eine polymorphe Klasse ist (d.h. eine Nicht-Union-Klasse, die mindestens eine virtuelle Funktion deklariert oder erbt), liefert die Member-Konstante value den Wert true . Für jeden anderen Typ ist value gleich false .

Wenn T ein unvollständiger Nicht-Union-Klassentyp ist, ist das Verhalten undefiniert.

Wenn das Programm Spezialisierungen für std::is_polymorphic oder std::is_polymorphic_v hinzufügt, ist das Verhalten undefiniert.

Inhaltsverzeichnis

Template-Parameter

T - ein zu prüfender Typ

Hilfsvariablen-Template

template < class T >
constexpr bool is_polymorphic_v = is_polymorphic < T > :: value ;
(seit C++17)

Geerbt von std:: integral_constant

Member-Konstanten

value
[static]
true falls T ein polymorpher Klassentyp ist, false andernfalls
(öffentliche statische Member-Konstante)

Member-Funktionen

operator bool
konvertiert das Objekt zu bool , gibt value zurück
(öffentliche Member-Funktion)
operator()
(C++14)
gibt value zurück
(öffentliche Member-Funktion)

Member-Typen

Typ Definition
value_type bool
type std:: integral_constant < bool , value >

Mögliche Implementierung

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)) {};

Beispiel

#include <type_traits>
struct A { int m; };
static_assert(!std::is_polymorphic_v<A>);
struct B { virtual void foo(); };
static_assert(std::is_polymorphic_v<B>);
struct C : B {};
static_assert(std::is_polymorphic_v<C>);
struct D { virtual ~D() = default; };
static_assert(std::is_polymorphic_v<D>);
// Verwendet Vererbung, aber nicht das virtual-Schlüsselwort:
struct E : A {};
static_assert(!std::is_polymorphic_v<E>);
struct F : virtual A {};
static_assert(!std::is_polymorphic_v<F>);
struct AX : A {};
struct AY : A {};
struct XY : virtual AX, virtual AY {};
static_assert(!std::is_polymorphic_v<XY>);
int main() {}

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrigiertes Verhalten
LWG 2015 C++11 das Verhalten war undefiniert falls
T ein unvollständiger Union-Typ ist
die Basis-Charakteristik ist
std::false_type in diesem Fall

Siehe auch

(C++11)
prüft, ob ein Typ ein Nicht-Union-Klassentyp ist
(Klassen-Template)
prüft, ob ein Typ ein abstrakter Klassentyp ist
(Klassen-Template)
prüft, ob ein Typ einen virtuellen Destruktor hat
(Klassen-Template)