1

Consider the following piece of C++ code:

void foo() {
  std::cout << "Hello, "
    << "world!"
    << std::endl;
}

When I run clang-format without any style options, I get this:

void foo() {
  std::cout << "Hello, "
            << "world!" << std::endl;
}

How to obtain the following result (each << starts on its own line)?

void foo() {
  std::cout << "Hello, "
            << "world!"
            << std::endl;
}

One advice is to terminate each line with //:

void foo() {
  std::cout << "Hello, " //
    << "world!" //
    << std::endl; //
}

But is that possible to achieve such indentation by using style options only?

Alexander Pozdneev
  • 1,289
  • 1
  • 13
  • 31

1 Answers1

3

https://reviews.llvm.org/D80950 explains why it does this. When clang-format was originally written, a decision was made that if literal strings were streamed next to each other i.e.

os << "A" << "B"

they would be put on a separate line

os << "A" 
   << "B"

but for anything else, they wouldn't

os << "A" << i << "B"

There really isn't any control over this. Breaking after every streaming operator could be considered too much.

os << "A"
   << i
   << "B"
Vojtěch Chvojka
  • 378
  • 1
  • 15
MyDeveloperDay
  • 2,485
  • 1
  • 17
  • 20