In algorithms where membership tests are frequent, I really miss the infix ∈ operator, and I like to use the % operator instead. It works well with the regular containers, but for some reason clang and gcc reject it for initializer_lists. I have no doubt they are "right" in the sense that the standard is certainly rejecting this. But why?
#include <initializer_list>
#include <iostream>
#include <vector>
/// Whether e ∈ m.
template <typename T, typename U>
inline bool
in(const U& k, const std::initializer_list<T>& m)
{
return std::find(begin(m), end(m), k) != std::end(m);
}
/// Whether e ∈ m.
template <typename T, typename U>
inline bool
operator%(const U& k, const std::initializer_list<T>& m)
{
return in(k, m);
}
/// Whether e ∈ m.
template <typename T, typename U>
inline bool
operator%(const U& k, const std::vector<T>& m)
{
return in(k, m);
}
int main()
{
using strings = std::vector<std::string>;
std::cout << ("foo" % strings{"foo", "bar"}) << std::endl;
std::cout << in("foo", {"foo", "bar"}) << std::endl;
std::cout << ("foo" % {"foo", "bar"}) << std::endl; // fails
}