Quick Links: |
Provide a compile-time check for determining polymorphic types. More...
Namespaces | |
namespace | bslmf |
namespace | bsl |
Defines | |
#define | bslmf_IsPolymorphic bslmf::IsPolymorphic |
bsl::is_polymorphic | standard meta-function for detecting polymorphic types |
bsl::is_polymorphic_v | the result value of the meta-function |
bslmf::IsPolymorphic | meta-function for detecting polymorphic types |
bsl::is_polymorphic
and BloombergLP::bslmf::IsPolymorphic
and a template variable bsl::is_polymorphic_v
, that represents the result value of the bsl::is_polymorphic
meta-function. All of these meta-functions may be used to query whether a type is a polymorphic class as defined in the C++11 standard [class.virtual]. A class is polymorphic if it has at least one virtual function. Note that the destructor of such a class should always be declared virtual
. Therefore, another definition of polymorphic is whether a class has a virtual destructor. bsl::is_polymorphic
has the same syntax as the is_polymorphic
template defined in the C++11 standard [meta.unary.prop], while bslmf::IsPolymorphic
was devised before is_polymorphic
was standardized. bsl::is_polymorphic
meets the requirements of the C++11 standard with two exceptions: The compilation will fail if the meta-function is used to evaluate a union
type, unless one of the following compilers, which provide an intrinsic operation to detect this specific trait, is used:
The meta-function will yield false positives, claiming non-polymorphic types are polymorphic, for types using virtual inheritance. This case is known to be handled correctly for the following compilers only:
bsl::is_polymorphic
is indicated by the class member value
, while the result for bslmf::IsPolymorphic
is indicated by the class member VALUE
. bsl::is_polymorphic
should be preferred over bslmf::IsPolymorphic
, and in general, should be used by new components. is_polymorphic_v
is defined in the C++17 standard as an inline variable. If the current compiler supports the inline variable C++17 compiler feature, bsl::is_polymorphic_v
is defined as an inline constexpr bool
variable. Otherwise, if the compiler supports the variable templates C++14 compiler feature, bsl::is_polymorphic_v
is defined as a non-inline constexpr bool
variable. See BSLS_COMPILERFEATURES_SUPPORT_INLINE_VARIABLES
and BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
macros in bsls_compilerfeatures component for details. MyStruct
and MyDerivedStruct
: struct MyStruct { void nonvirtualMethod(); }; struct MyDerivedStruct : public MyStruct { };
MyClass
and MyDerivedClass
: class MyClass { public: MyClass(); virtual ~MyClass(); // makes 'MyClass' polymorphic }; class MyDerivedClass : public MyClass { public: MyDerivedClass(); virtual ~MyDerivedClass(); };
bsl::is_polymorphic
: assert(false == bsl::is_polymorphic<MyStruct >::value); assert(false == bsl::is_polymorphic<MyStruct *>::value); assert(false == bsl::is_polymorphic<MyDerivedStruct& >::value); assert(false == bsl::is_polymorphic<MyDerivedStruct *>::value); assert(true == bsl::is_polymorphic< MyClass >::value); assert(false == bsl::is_polymorphic<const MyClass& >::value); assert(false == bsl::is_polymorphic< MyClass *>::value); assert(true == bsl::is_polymorphic<MyDerivedClass >::value); assert(false == bsl::is_polymorphic<MyDerivedClass& >::value); assert(false == bsl::is_polymorphic<MyDerivedClass *>::value);
bsl::is_polymorphic_v
variable as follows: #ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES assert(false == bsl::is_polymorphic_v<MyStruct >); assert(false == bsl::is_polymorphic_v<MyStruct *>); assert(false == bsl::is_polymorphic_v<MyDerivedStruct& >); assert(false == bsl::is_polymorphic_v<MyDerivedStruct *>); assert(true == bsl::is_polymorphic_v< MyClass >); assert(false == bsl::is_polymorphic_v<const MyClass& >); assert(false == bsl::is_polymorphic_v< MyClass *>); assert(true == bsl::is_polymorphic_v<MyDerivedClass >); assert(false == bsl::is_polymorphic_v<MyDerivedClass& >); assert(false == bsl::is_polymorphic_v<MyDerivedClass *>); #endif
#define bslmf_IsPolymorphic bslmf::IsPolymorphic |