
On Wed, Nov 23, 2011 at 6:54 AM, Joel de Guzman <joel@boost-consulting.com> wrote:
On 11/23/2011 7:06 PM, Lorenzo Caminiti wrote:
just using phoenix and/or lambda :
int max = 0; auto small = _1 < max; // (2)
Or even better:
int max = 0; auto small = [max](int x) { return x < max; } // (3)
IMO (and others have second this point), (3) is better than (2) because at some point (3+ years from now?) everyone will be comfortable with C++11 lambda syntax (3) because it's part of the standard while there will still be people on your team that will have no idea what you meant in writing (2). By the same token, the syntax of (1) (statement syntax for the body) can be considered better than the one of (2) even if (1) is more verbose and than (2) (verbosity which might be a plus in the context to make the syntax more clear-- in fact (3) is more verbose than (2) but it is the new standard). All of that said, if the a syntax is more or less understandable is a subjective matter so I recognize that you and/or Hartmut feel differently on this topic.
No it's not better. First: (3) is not polymorphic! (2) can be reused regardless of the type of _1. To make it clear:
auto less = _1 < _2;
The above can be reused in any expression as long as they have the less-than relation. E.g. less(i, 123), less(str, "hello), etc. It's these simple 1-2-3 liners where Phx/Lambda shine. You'd have to be absurdly dumb to get those expressions wrong!
Second: (2) is more succinct.
For some of our users/problems polymorphism might not be relevant while using statement syntax for the body might. For those users/problems (3) might be worst than (2) because is does not use statement syntax in its body (worst error messages + less readable by others). We leave that decision up to our library users and we are all content. --Lorenzo