Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs indenting of C++11 lambda functions (cc-mode)

The default Emacs C++ mode (cc-mode) still does not recognize many C++11 features. One annoying issue is that it applies too much indentation to lambda functions used as function parameters:

std::vector<int> ar(4);
std::generate_n(std::begin(ar), 4, [] {
        static int g_i;
        return g_i++;
    });
std::for_each(std::begin(ar), std::end(ar), [](int i) {
        std::cout << " " << i;
    });
bool b = std::is_sorted(std::begin(ar), std::end(ar), [&](int l, int r) {
        return l<r;
    });
std::cout << "   " << b << "\n";

Ideally, one would prefer:

std::vector<int> ar(4);
std::generate_n(std::begin(ar), 4, [] {
    static int g_i;
    return g_i++;
});
std::for_each(std::begin(ar), std::end(ar), [](int i) {
    std::cout << " " << i;
});
bool b = std::is_sorted(std::begin(ar), std::end(ar), [&](int l, int r) {
    return l<r;
});
std::cout << "   " << b << "\n";

Are there good solutions for this?

like image 464
Hugues Avatar asked May 08 '14 22:05

Hugues


1 Answers

In Emacs26, the accepted answer doesn't work for me anymore. I just set the 'inlambda' to 0.

  (c-offsets-alist . ((case-label . 0)
                      (inline-open . 0)
                      (substatement-open . 0)
                      (inlambda . 0) ; no extra indent for lambda
                      (block-open . 0) ; no space before {
                      (knr-argdecl-intro . -)))
like image 124
frinkr Avatar answered Oct 23 '22 11:10

frinkr