Search code examples
c++c++20c++-conceptsclang-format

Using clang-format with c++20 concepts


I have looked at the clang-format style options https://clang.llvm.org/docs/ClangFormatStyleOptions.html but don't see any reference to c++ concepts and requires clauses. Normally I can configure clang-format to do what I want but I can't figure out how to get it to handle my concepts and requires clauses nicely:

  1. Currently clang-format does this to my concepts:
template <typename F, typename P, typename T>
concept Accumulate_Fn = Parser<P>&& std::invocable<F, T, parser_t<P>>&&
    std::same_as<T, std::invoke_result_t<F, T, parser_t<P>>>;

But I would like to put each one constraint on its own line (as it does for function arguments that get too long) so that the result would look like:

template <typename F, typename P, typename T>
concept Accumulate_Fn = Parser<P> &&
                        std::invocable<F, T, parser_t<P>> &&
                        std::same_as<T, std::invoke_result_t<F, T, parser_t<P>>>;
  1. For a function with a requires clause, clang-format currently gives me:
template <Parser P1, Parser P2, typename T, Accumulate_Fn<P1, parser_t<P1>> F>
requires std::same_as<T, parser_t<P1>> constexpr Parser auto
separated_by(P1&& p1, P2&& p2, T&& init, F&& f)

But I would like something much closer to :

template <Parser P1, Parser P2, typename T, Accumulate_Fn<P1, parser_t<P1>> F>
requires std::same_as<T, parser_t<P1>> 
constexpr Parser auto separated_by(P1&& p1, P2&& p2, T&& init, F&& f)

Are there any magical options that will make that work? I'm currently on clang-format 10.0.


Solution

  • As of Jul/20, Concepts are not properly supported by clang-format. There is an open issue in LLVM tracker.