Emacs indentation support for C++11 syntax?
c++11, emacs
Solution
I just manually installed the latest CC Mode 5.33 from SourceForge, which should cover most of what you are looking for:
C++11 should now be fully supported, along with some features of C++14:
- Uniform initialisation
- Lambda functions
- Parameter packs
- Raw strings
- Separators in integer literals
- ">>" as a double template ender
- etc.
Here are the indentations I get for your examples:
struct m {
int a;
char b;
};
std::vector<m> foo { {1, 'a'},
{2, 'b'},
{ 3, 'c'},
{ 4, 'd' }};
and
cout << 5
<< [](int a) {
return 2 * a;
} (5);
I also recommend installing `modern-c++-font-lock` (e.g. via MELPA) as suggested in this SO answer.
Problem
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.)