Emacs in its default configuration seems to have trouble indenting C++ methods with reference qualifiers (& and &&). For example:

struct foo {
    void a_method() && {
                        std::cout << "rvalue ref\n";

    void a_method() const& {
                            std::cout << "lvalue const ref\n";

    void this_gets_properly_indented() const {
        std::cout << "without a ref-qualifier\n";

    auto this_too() && -> void {
        std::cout << "rvalue ref; '->' notation\n";

One workaround I have found is to use the -> syntax for return types, but this breaks too if the return type is a reference. Can this behaviour be somehow configured?

  • Your example does work for me with Emacs 26.1, and c-indentation-style "visual". – Tobias Apr 5 at 14:41
  • @Tobias Ha! Calling (c-set-style "visual") actually fixed it. I do have 26.1 too, and c-indentation-style was "gnu" by default. I can't find any documentation on this "visual" style though. It's not in the manual. =/ – ea7ababe Apr 5 at 20:42
  • "linux", "bsd" and "java" styles also seem to give good results. – ea7ababe Apr 5 at 21:01
  • I think, I have localized it further. In "gnu" style there is a brace-list-intro item in c-offsets-alist, which is set to c-lineup-arglist-intro-after-paren. When I set it to + instead it seems to fix the problem. But, as I understand it, brace-list-intro is used for initializer lists, not code blocks. :| It definitely looks like an error. – ea7ababe Apr 5 at 21:40
  • cc-mode is built-in. Your problem is not listed in cc-mode limitations and a search for indent AND ref AND qualifier in the cc-mode-help mailing list does not give relevant results. If you think you found a bug please report it either with M-x report-emacs-bug or at bug-cc-mode@gnu.org. Thanks for making Emacs better. – Tobias Apr 7 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.