Search code examples
c++clangclang-format

can clang-format sort "using namespace <...> " alphabetically?


Say, I have a bunch of .cc source files with statements like

using namespace lib::a;
using namespace lib::d;
using namespace lib::c;
using namespace lib::b;

I want them to be sorted

using namespace lib::a;
using namespace lib::b;
using namespace lib::c;
using namespace lib::d;

using a .clang-format file.

Is there a key value pair to achieve this? I know that this works for header files, but I have not seen it for sorting namespaces.


Solution

  • For current (latest) clang-format version the answer is: Unfortunately, No.
    Although it has great "include" sorting features, there is no key for "using"s.

    If the feature is added, it will be mentioned in the Clang-format Documentation

    And also I don't recommend using "using namespace". As it is dangerous and is considered as bad practice.
    See the warnings:

    1. Topic 1
    2. Topic 2