Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

enable_if_

template<bool B, class T = void>
struct enable_if_;

template<bool B, class T = void>
using enable_if_t = typename enable_if_<B, T>::type;

type: If B is true, then the member type is defined to be T. Otherwise there is no member type.

Header: #include <boost/type_traits/enable_if.hpp>

[Note] Note

The trait has the name enable_if_ (with a trailing underscore) but behaves like std::enable_if or boost::enable_if_c. The existing trait with the name boost::enable_if has a different interface.

Examples:

The following function can be used to destroy each element of an array and specially handle arrays of trivially destructible types.

template<class T>
typename boost::enable_if_<!boost::has_trivial_destructor<T>::value>::type
destroy(T* ptr, std::size_t size)
{
    while (size > 0) {
        ptr[--size].~T();
    }
}

template<class T>
typename boost::enable_if_<boost::has_trivial_destructor<T>::value>::type
destroy(T*, std::size_t) { }

Compiler Compatibility: All current compilers are supported by this trait. The type alias enable_if_t is only available if the compiler supports template aliases.


PrevUpHomeNext