Last active
January 18, 2016 23:21
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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