I have a vector of vectors, and I want to check if all of them are empty. Using the standard library, I tried:
#include <algorithm>
#include <vector>
int main()
{
std::vector<std::vector<int>> vv;
std::all_of(std::begin(vv), std::end(vv), std::empty);
}
This result in the following error in clang 7.0:
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_algo.h:508:5: note: candidate template ignored: couldn't infer template argument '_Predicate'
I guess it's a standard behavior, due to the rules of type deduction. But anyway, what i the easiest way to workaround this?
EDIT: I accepted rubenvb's answer, because he gave a simple and reasonable explanation, together with the natural workaround. all_of accepts a predicate, which is a function, a function object or a lambda expression. std::empty is neither of those, but a function template. When explicitly instantiating it, we get a plain function which should work. Surprisingly, it's still not compile on most compilers I tried.
Well, lets see:
on GCC 6.3, it compiles just fine - https://godbolt.org/g/Pxta7C
but on GCC from trunk, it causes an internal compiler error - https://godbolt.org/g/H6DHt5
Neither Clang from trunk or MSVC 2017 succeed to compile it:
https://godbolt.org/g/819pbQ (Clang)
https://godbolt.org/g/ua5E8e (MSVC)
EDIT2: Apparently, Robert Andrzejuk is right too: the reason that the compiler cannot handle it is an ambiguous overload resolution. std::empty has 3 different overloads. and two of them are equally well candidates: the general one and the std::initializer list one. I achieved similar results with the following minimal version:
#include <vector>
template<class T>
void foo(const T& t);
template<class T>
void foo(const std::initializer_list<T>& il);
template<class F>
void bar(F f);
int main()
{
bar(foo<std::vector<int>>);
}
There is one difference, though. This example simply not compile in GCC from trunk (instead of causing an ICE).