1

Given the following file, named as x.cpp I get the following unexpected indentation behavior:

#include <stdio.h>
class X {
public:
     X()
          {  // <-- Why the indent?
               printf("Hello!");
          }
};
int main() { X x; }

// Local Variables:
// c-file-style: "k&r"
// End:

I know that this file is not valid C source, but if I rename the file to x.c and re-indent, I suddenly get the expected indentation:

#include <stdio.h>
class X {
public:
     X()
     {  // <-- Looks good
          printf("Hello!");
     }
};
int main() { X x; }

// Local Variables:
// c-file-style: "k&r"
// End:

Obviously, the problem is a difference in the way the file is parsed syntactically in the two different major modes. In C mode, the brace on line 5 is seen as substatement-open, while in C++ mode it is seen as inline-open. Since the emacs definition of "k&r" style doesn't specify an indent for inline-open it defaults to something silly.

My question then, is whether it is reasonable to expect that "k&r" indentation style should work in C++. In other words, should I file an emacs bug for this? Or am I missing something that would make this work as expected?

  • I discovered that "bsd" style is a good substitute for "k&r" that seems to play nicer with C++. – nispio Feb 14 '17 at 20:38

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.