BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmf_ispolymorphic

Macros

#define bslmf_IsPolymorphic   bslmf::IsPolymorphic
 This alias is defined for backward compatibility.
 

Detailed Description

Outline

Purpose

Provide a compile-time check for determining polymorphic types.

Classes

Description

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:

  1. 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:
    • gcc 4.3 or later
    • Visual C++ 2008 or later
  2. 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:
    • Compilers with intrinsic support, listed above
    • IBM XLC

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.

Usage

In this section we show intended use of this component.

Example 1: Verify Polymorphic Types

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:

struct MyStruct {
void nonvirtualMethod();
};
struct MyDerivedStruct : public MyStruct {
};

Then, we define two types in a polymorphic hierarchy, MyClass and MyDerivedClass:

class MyClass {
public:
MyClass();
virtual ~MyClass(); // makes 'MyClass' polymorphic
};
class MyDerivedClass : public MyClass {
public:
MyDerivedClass();
virtual ~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:

#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

Macro Definition Documentation

◆ bslmf_IsPolymorphic

#define bslmf_IsPolymorphic   bslmf::IsPolymorphic