Skip to content

Instantly share code, notes, and snippets.

@ldionne
Last active January 18, 2016 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldionne/8d859cc61a634321df2c to your computer and use it in GitHub Desktop.
Save ldionne/8d859cc61a634321df2c to your computer and use it in GitHub Desktop.
Implementation of a lazy conjunction based on SFINAE (see DR 1227: http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1227)
// Note: This original idea was shown to me by Eric Fiselier. All credits where due.
#include <type_traits>
template <typename Condition, typename T = void>
struct enable_if
: std::enable_if<Condition::value, T>
{ };
std::true_type expand(...);
template <typename ...T>
decltype(expand(typename enable_if<T, void*>::type{}...)) and_impl(int);
template <typename ...T>
std::false_type and_impl(...);
template <typename ...T>
struct and_
: decltype(and_impl<T...>(int{}))
{ };
template <typename ...T>
struct Fails {
static_assert(sizeof...(T) && false, "do not instantiate");
static constexpr bool value = true;
};
static_assert(and_<std::true_type, std::true_type>::value, "");
static_assert(!and_<std::false_type, Fails<>>::value, "");
static_assert(!and_<std::true_type, std::false_type, Fails<>>::value, "");
int main() { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment