Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indentation in the presence of requires-clauses #5046

Open
tkoeppe opened this issue Oct 21, 2021 · 7 comments
Open

Indentation in the presence of requires-clauses #5046

tkoeppe opened this issue Oct 21, 2021 · 7 comments

Comments

@tkoeppe
Copy link
Contributor

tkoeppe commented Oct 21, 2021

We are inconsistent in how we indent subsequent lines when requires-clauses are present. We should update the wiki to include requires-clauses, and fix the indentation where needed.

Recall the established conventions:

  • Class templates do not indent after the template head:

    template<class T>
    struct is_correctly_formatted;
    
  • Function templates are indented after the template head:

    template<class T>
      constexpr size_t indent_for(T&& val);
    

Now enter requires-clauses. We seem to be pretty consistent in indenting the requires-clause itself. So I propose to mix that in as follows:

  • For class templates, the class key itself is unindented, even though there's intervening indentation for the requires-clause:

    template<class T>
      requires literary_type<T>
    struct is_correctly_formatted;
    
  • For function templates, the function is indented by one more level:

    template<class T>
      requires literary_type<T>
        constexpr size_t indent_for(T&& val);
    

An alternative would be to not add extra indentation to function templates (so everything below the template head would just be indented by one level), but we generally seem to like increasing indentation depth for function templates.

Thoughts?

@tkoeppe
Copy link
Contributor Author

tkoeppe commented Oct 21, 2021

The main amount of review work seems to be in ranges.tex and concepts.tex.

@jensmaurer
Copy link
Member

We say

In a header synopsis, declarations of class and function templates should have a linebreak+indent after the template<...> head. In a class template definition, there is no additional indent after the template<...> head, and the linebreak after the template head is optional.

in our guidelines, so we always indent declarations (including class template declarations), but we don't indent class template definitions.

Your first example is thus wrong:

  template<class T>
  struct is_correctly_formatted;

@jensmaurer jensmaurer added the decision-required A decision of the editorial group (or the Project Editor) is required. label Oct 21, 2021
@tkoeppe
Copy link
Contributor Author

tkoeppe commented Oct 21, 2021

Ah yes, indeed. Bad example then, but I think we have analogous violations for class template definitions.

@tkoeppe
Copy link
Contributor Author

tkoeppe commented Jun 12, 2023

Editorial meeting observations:

  • A template head introduces a scope, which justifies indenting the subsequent code.
  • A requires-clause does not.
  • We could not indent either requires-clause or return type.

Another alternative: indent requires-by four spaces:

template<typename T>
    requires(foo<T>)
  int main();

We should also check how function definitions would look.

@tkoeppe
Copy link
Contributor Author

tkoeppe commented Jun 12, 2023

Tangential note on braces: Either full braces or no braces, the latter only when both arms fit on a single line (not: statement).

@tkoeppe
Copy link
Contributor Author

tkoeppe commented Jun 12, 2023

Tangential note on late requires clauses of function definitions: Indent by two, like the function body:

constexpr auto foo() const
  requires(is_same<T, T>) {
  return T() + T();
}

@jensmaurer jensmaurer removed the decision-required A decision of the editorial group (or the Project Editor) is required. label Jun 12, 2023
@tkoeppe
Copy link
Contributor Author

tkoeppe commented Jun 12, 2023

Editorial meeting consensus:

  • (See 29.5.7 (<=> for an example):) For function declarations, both the early requires-clause and the return type should be on the same indent level (one level). The line with the return type should be short enough that the return type does indeed appear on the next line after the requires, i.e. no overly long explicit(very long...).
  • Don't have both an early and a trailing requires clause (check with LWG).
  • It's OK to not have a line break at all for late requires clause if it's short enough to fit.
  • Update the wiki re all of the above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants