Skip to content

Template metaprogramming helpers

Raymond Chen edited this page Jul 23, 2019 · 1 revision

WIL contains a small number of template metaprogramming helpers.

Usage

The template metaprogarmming helpers are defined in wil/common.h.

#include <wil/common.h>

variadic_logical_or

template<bool...> struct variadic_logical_or
{
    static const bool value; // true if any template parameter is true
};

The variadic_logical_or structure derives from wistd::true_type if any of the boolean template parameters is true. Otherwise, it derives from wistd::false_type.

Starting in C++17, this metaprogramming helper is largely unnecessary. A similar effect can be obtained by using the std::disjunction class or a fold expression.

Examples

enum class WidgetColor
{
   Red, White, Blue, Black
};

template<
    WidgetColor color,
    bool isColorful = variadic_logical_or<color == WidgetColor::Red,
                                          color == WidgetColor::Blue>::value>
struct Widget
{
    ...
};