|
BDE 4.14.0 Production release
|
Macros | |
| #define | bslmf_IsPolymorphic bslmf::IsPolymorphic |
| This alias is defined for backward compatibility. | |
Provide a compile-time check for determining polymorphic types.
This component defines two meta-functions, 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:
union type, unless one of the following compilers, which provide an intrinsic operation to detect this specific trait, is used:The two meta-functions are functionally equivalent. The major difference between them is that the result for 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.
Note that the template variable 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.
In this section we show intended use of this component.
Suppose that we want to assert whether a particular type is a polymorphic type.
First, we define two types in a non-polymorphic hierarchy, MyStruct and MyDerivedStruct:
Then, we define two types in a polymorphic hierarchy, MyClass and MyDerivedClass:
Now, assert that the two types in the non-polymorphic hierarchy are not polymorphic, and that the two types in the polymorphic hierarchy are polymorphic using bsl::is_polymorphic:
Note that if the current compiler supports the variable templates C++14 feature then we can re-write the snippet of code above using the bsl::is_polymorphic_v variable as follows:
| #define bslmf_IsPolymorphic bslmf::IsPolymorphic |