Skip to content

Instantly share code, notes, and snippets.

@vendethiel
Last active August 29, 2015 14:03
Show Gist options
  • Save vendethiel/f45190f52fcbbb183fd6 to your computer and use it in GitHub Desktop.
Save vendethiel/f45190f52fcbbb183fd6 to your computer and use it in GitHub Desktop.
Any.cpp
#include <utility>
#include <vector>
#include <iostream>
#define ANY_OP(T, OP) bool operator OP(T el) { \
for (const auto& elem : _elems) { \
if (elem OP el) return true; \
} \
return false; \
}
template<class T>
class Any {
public:
Any(std::initializer_list<T> elems) : _elems { elems }
{ }
Any(std::vector<T> elems) : _elems { std::move(elems) }
{ }
ANY_OP(T, ==)
ANY_OP(T, >)
ANY_OP(T, <)
private:
std::vector<T> _elems;
};
template<typename T, std::size_t N>
Any<T> any(T const (&data)[N])
{
return Any<T> {{ data, data + N }};
}
int main() {
std::cout << (any({ 1, 2, 3 }) == 1) << std::endl;
std::cout << (any({ 10, 11, 12 }) < 10) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment