On 06.12.2016 14:03, Ralf Goertz wrote:
>
> is it possible to have a lambda expression as a template argument?
A lambda that doesn't capture converts willingly to pointer to function,
which could have been a template argument except for
* there is no such conversion for template arguments, and
* the standard enumerates what kinds of values can be template
arguments, as quoted by Stefan Ram else-thread, and lambdas are not in
that list.
> If not why not?
See above.
> I get an error when I try to compile the following
>
> typedef std::set< std::set<int>,
> [](const std::set<int>&x,const std::set<int>&y){
> return x.size()==y.size() ? x<y : x.size()<y.size();
> }> Setset;
>
> error: lambda-expression in template-argument
>
> Background is that I'd rather have sets with fewer elements sorted
> before sets with more elements.
[code]
#include <set>
auto main()
-> int
{
using namespace std;
using Int_set = set<int>;
using Comparator_func = auto(*)(Int_set const&, Int_set const&) ->
bool;
using Set_of_sets = set<Int_set, Comparator_func>;
Set_of_sets blah{ [](Int_set const&x, Int_set const& y )
{
return x.size()==y.size() ? x<y : x.size()<y.size();
} };
}
[/code]
Cheers & hth.,
- Alf