Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs indentation support for C++11 syntax?

Tags:

People also ask

How do I indent multiple lines in Emacs?

Select multiply lines, then type C-u 8 C-x Tab , it will indent the region by 8 spaces.


Now that C++11 is out, I wondered if there are any tips on improving indentation support in Emacs when more and more code is migrated from C++98 to C++11.

Examples:

  • Parentheses are often becoming braces, which seem to confuse Emacs.
  • Lambda expressions are not well supported yet
  • Trailing type function declarations (cannot comment, as I have not used them)
  • Variadic templates (though personally, I had no problem with them so far)

Here are some questionable indentions that I find myself working around:

struct m {
    int a;
    char b;
};

std::vector<m> foo { {1, 'a'},
        {2, 'b'},
            { 3, 'c'},
                { 4, 'd' }};

I'd prefer

std::vector<m> foo { {1, 'a'},
                     {2, 'b'},
                     { 3, 'c'},
                     { 4, 'd' }};

or even

std::vector<m> foo { {1, 'a'},
        {2, 'b'},
        { 3, 'c'},
        { 4, 'd' }};

for example.

Next one:

cout << 5
     << [](int a) {
    return 2 * a;
} (5);

I'd prefer

cout << 5
     << [](int a) {
            return 2 * a;
        } (5);

so that the block is indented relative to the lambda.

I find myself spending more time on indentation, which is annoying.

Are any packages or customizations that help to indent modern C++11 code?

(side note: I set up clang-format for Emacs, but I cannot get 100% compatibility which existing code and it also does not yet understand C++11 syntax very well. Still it is useful sometimes and sounds like a good idea for new projects.)