c++lambdaemacsindentationcc-mode

CC-mode indentation of C++ lambda functions with a return type that has template parameters


I am using CC-mode in Emacs that was installed with apt on Ubuntu 20.04. It formats code as follows:

#include <vector>
int main() {
  auto f1 = [](int a) {
              return std::vector<int>({a});
            };
  auto f2 = [](int a) -> std::vector<int> {
                                           return std::vector<int>({a});
  };
}

In my opinion, format for f1 is acceptable but f2 not. Is there a way to have it format f2 as in f1? Or, more preferably, as in the following, which was achieved with CC-mode installed on Ubuntu 16.04.

#include <vector>
int main() {
  auto f1 = [](int a) {
    return std::vector<int>({a});
  };
  auto f2 = [](int a) -> std::vector<int> {
    return std::vector<int>({a});
  };
}

My current workaround is as follows, but not ideal:

  using vector_t = std::vector<int>;
  auto f2wa = [](int a) -> vector_t {
                return vector_t({a});
              };

Solution

  • If you use google-set-c-style.el from melpa you get your preferred formatting,

    #include <vector>
    int main() {
      auto f1 = [](int a) {
        return std::vector<int>({a});
      };
      auto f2 = [](int a) -> std::vector<int> {
        return std::vector<int>({a});
      };
    }
    

    In general, with cc-modes, you can play around with c-guess to examine and set the style rules until you find what you want.