TMPでboolの計算 (memo)

trueとfalseをポインタ型と非ポインタ型に対応させることでパターンマッチが使える。再帰深度がO(1)に抑えられる。ユースケースとしては可変長引数テンプレートパラメータを受け取るコンテナとかのメンバ関数のnoexcept指定とか色々。

[Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ

#include <type_traits>

namespace detail {

template <typename... Types>
struct and_ {
    static constexpr bool value = false;
};

template <typename... Types>
struct and_<Types*...> {
    static constexpr bool value = true;
};
    
}  // detail

template <bool... Bs>
struct and_ {
    static constexpr bool value = detail::and_<
        typename std::conditional<Bs, int*, int>::type...>::value;
};

int main() {
    static_assert(and_<true, true, true>::value, "");
    static_assert(!and_<false, true, true>::value, "");
}