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

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

is_detected

template<template<class...> class Op, class... Args>
using is_detected = see-below;

template<template<class...> class Op, class... Args>
constexpr bool is_detected_v = is_detected<Op, Args...>::value;

Aliases: If Op<Args...> is a valid template-id, aliases true_type, otherwise aliases false_type.

C++ Standard Paper: N4502

Compiler Compatibility: Requires C++11 variadic templates and C++11 template aliases.

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

Examples:

Suppose we wish to "reset" a value of type T, if the type has a clear() member function then we should call it, otherwise we should assign a default constructed value:

template<class T>
using clear_t = decltype(boost::declval<T&>().clear());

template<class T>
void clear(T& value)
{
    if constexpr (boost::is_detected_v<clear_t, T>) {
        value.clear();
    } else {
        value = T();
    }
}

See also: is_detected_convertible, is_detected_exact.


PrevUpHomeNext