c++coding-styleclang-format

Wrap long C++ function heads with clang-format


I just started to use clang-format and I could set up quite a lot of things, but I am stuck with my general handling of long function headers. This is how I usually create them:

void myLongFunctionOrMethod(
    int one,
    float two,
    std::string three,
    ...whatever
) {
    Do something
    ...
}

To me, this is the cleanest and most maintainable style. Is there a way to enforce this in clang-format? I prefer not to place braces on the next line for various reasons, but I find it best when the (outer) indentation level clearly indicates where the body begins.


Solution

  • ---
    Language:        Cpp
    AlignAfterOpenBracket: BlockIndent
    AllowAllParametersOfDeclarationOnNextLine: false
    BinPackParameters: false
    

    This combination of settings should do what you want.

    Before:

    #include <iostream>
    #include <string>
    
    void myLongFunctionOrMethod(int one, float two, std::string three, int four,
                                int five, int six, int seven) {
      std::cout << "Hi.\n";
    }
    
    int main() {}
    

    After:

    #include <iostream>
    #include <string>
    
    void myLongFunctionOrMethod(
        int one,
        float two,
        std::string three,
        int four,
        int five,
        int six,
        int seven
    ) {
      std::cout << "Hi.\n";
    }
    
    int main() {}
    

    This was tested using clang-format version 18.1.6 (Fedora 18.1.6-3.fc40).