Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DRY Multicomparisons
template <typename Type, typename ComparisonFunc = std::equal_to<Type>>
class is
{
public:
constexpr is(Type t)
: m_Value(std::move(t))
{}
bool any_of(const std::initializer_list<Type> &list) const
{
return std::any_of(list.begin(), list.end(), std::bind(ComparisonFunc(), m_Value, std::placeholders::_1));
}
bool any_of(const std::initializer_list<Type> &&list) const
{
return std::any_of(list.begin(), list.end(), std::bind(ComparisonFunc(), m_Value, std::placeholders::_1));
}
bool all_of(const std::initializer_list<Type> &list) const
{
return std::all_of(list.begin(), list.end(), std::bind(ComparisonFunc(), m_Value, std::placeholders::_1));
}
bool all_of(const std::initializer_list<Type> &&list) const
{
return std::all_of(list.begin(), list.end(), std::bind(ComparisonFunc(), m_Value, std::placeholders::_1));
}
bool none_of(const std::initializer_list<Type> &list) const
{
return std::none_of(list.begin(), list.end(), std::bind(ComparisonFunc(), m_Value, std::placeholders::_1));
}
bool none_of(const std::initializer_list<Type> &&list) const
{
return std::none_of(list.begin(), list.end(), std::bind(ComparisonFunc(), m_Value, std::placeholders::_1));
}
private:
Type m_Value;
};
@Furkanzmc

This comment has been minimized.

Copy link
Owner Author

@Furkanzmc Furkanzmc commented Sep 2, 2018

Example Usage:

int main(int /*argc*/, const char */*argv*/[])
{
    std::initializer_list<int> lvalueList{1, 2, 3};
    std::cout << is<int>(2).any_of(lvalueList) << std::endl; // Outputs 1
    std::cout << is<int, std::greater<int>>(5).all_of({1, 2, 3}) << std::endl; // Outputs 1
    std::cout << is<int, std::less<int>>(0).all_of({1, 2, 3}) << std::endl; // Outputs 1
    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment