On 1/10/24 16:41, Marek Polacek wrote:
On Wed, Jan 10, 2024 at 04:24:42PM -0500, Jason Merrill wrote:
On 1/10/24 15:59, Marek Polacek wrote:
On Wed, Jan 10, 2024 at 02:58:03PM -0500, Jason Merrill via Gcc wrote:
What formatting style do we want for non-trivial lambdas in GCC sources?
I'm thinking the most consistent choice would be

auto l = [&] (parms) // space between ] (
    {                  // brace on new line, indented two spaces
      return stuff;
    };

Sure, why not.  Consistency is what matters.  Thus far we seem
to have been very inconsistent.  ;)
By default, recent emacs lines up the { with the previous line, like an
in-class function definition; I talked it into the above indentation with

(defun lambda-offset (elem)
    (if (assq 'inline-open c-syntactic-context) '+ 0))
(add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))

I think we probably want the same formatting for lambdas in function
argument lists, e.g.

algorithm ([] (parms)
    {
      return foo;
    });

And what about lambdas in conditions:

if (foo ()
      && [&] (params) mutable
         {
         return 42;
         } ())

should the { go just below [?

I think we don't want the { to go below the [ in general; that was the old
emacs default behavior, and it produced lambda bodies with excessive
indentation.

With my adjustment above, emacs indents the { two spaces from the &&, which
seems a bit arbitrary but reasonable.

Fair enough, I think that's better.

I suppose we should add a note wrt lambdas to
https://gcc.gnu.org/codingconventions.html#Cxx_Conventions

Indeed, that's my goal.

Looking through the various lambdas already in the compiler, I see a good number already follow my suggested indentation, but a large majority omit the space between ] and (, so maybe we don't want it?

Jason

Reply via email to