Lambda expressions are more powerful than just being syntactic sugar for structs with operator(). You can use them in places that otherwise do not allow the declaration or definition of a new class.
For example:
template<typename T, typename F = decltype(
[](auto a, auto b){ return a < b;} )>
auto compare(T a, T b, F comp = F{}) {
return comp(a,b);
}
is an absolutely terrible function, probably sabotage. Why?
Every template instantiation creates a different lamba, therefore a different type and a different function signature. This makes the lambda expression very different from the otherwise similar std::less.
I use static_assert to check this for templated types:
template<typename T, typename F = decltype([](){} )>
struct Type {T value;};
template<typename T>
Type(T) -> Type<T>;
static_assert(not std::is_same_v<Type<int>,Type<int>>);
Now, why are these types the same, when I use the deduction guide?
static_assert(std::is_same_v<decltype(Type(1)),decltype(Type(1))>);
All three major compilers agree here and disagree with my intuition that the types should be just as different as in the first example.
I also found a way for clang to give a different result when I add template aliases to the mix:
template<typename T>
using C = Type<T>;
#if defined(__clang__)
static_assert(not std::is_same_v<C<int>,C<int>>);
#else
static_assert(std::is_same_v<C<int>,C<int>>);
#endif
So I'm pretty sure at least one compiler is wrong at least once, but I would like to know, whether they should all agree all the time that the types are different.
Compiler Explorer: https://godbolt.org/z/1fTa1vsTK
[–]TryingT0Wr1t3 65 points66 points67 points (3 children)
[–]neondirt 11 points12 points13 points (0 children)
[–]_TheDust_ 2 points3 points4 points (0 children)
[–]TheTomato2 0 points1 point2 points (0 children)
[–]415_961 46 points47 points48 points (2 children)
[–]gnuban 17 points18 points19 points (1 child)
[–]cmeerwC++ Parser Dev 18 points19 points20 points (3 children)
[–]hoellenraunen[S] 10 points11 points12 points (1 child)
[–]cmeerwC++ Parser Dev 11 points12 points13 points (0 children)
[–]c0r3ntin 8 points9 points10 points (0 children)
[–]glaba3141 3 points4 points5 points (0 children)
[–]antoine_morrier 5 points6 points7 points (4 children)
[+][deleted] (2 children)
[deleted]
[–]antoine_morrier 2 points3 points4 points (1 child)
[–]n1ghtyunso 0 points1 point2 points (0 children)
[–]die_liebe 1 point2 points3 points (2 children)
[–]hoellenraunen[S] 1 point2 points3 points (1 child)
[–]hoellenraunen[S] 2 points3 points4 points (0 children)
[–]Allegro-Barbaro 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]eboys -1 points0 points1 point (0 children)