Each lambda is an unrelated type. Be that as it may, all of them can be converted to std::function<void()> , but this is because std::function will require to convert something, and it will work when they are invokable with void() signature and copyable and destructible.
In the case of vector there is a constructor std::initializer_list<std::function<void()>> , which is considered from its list of constructors. This is a coincidence, attempt and compilation.
Without this argument (list argument to ctor vector), to match, the syntax {} instead scans its contents for a generic type. There is no ordinary type, so it fails.
The language does not search every type and pattern to find a possible common type between two (unrelated) lambdas. He will not read your mind.
You can do:
using nullary = std::function<void()>; template<class T> using il=std::initializer_list<T>; for(auto f:il<nullary>{[]{ std::cout<<"hello";},[]{std::cout<<" world\n";}}){ f(); }
source share