No, you cannot put it in decltype because
Lambda expression must not appear in an unpublished operand
You can do the following though
auto n = [](int l, int r) { return l > r; }; std::set<int, decltype(n)> s(n);
But it is really ugly. Note that each lambda expression creates a new unique type. If you subsequently do the following elsewhere, t is of a different type than s
auto n = [](int l, int r) { return l > r; }; std::set<int, decltype(n)> t(n);
You can use std::function , but note that this will entail a tiny runtime cost, since this requires an indirect call to the operator to call the lambda function object. This is probably negligible here, but can be significant if you want to pass function objects this way, for example, std::sort .
std::set<int, function<bool(int, int)>> s([](int l, int r) { return l > r; });
As always, first the code and then the profile :)
Johannes Schaub - litb Oct 05 '10 at 20:11 2010-10-05 20:11
source share