From: Simon <turner25@gmail.com>
To: 19867@debbugs.gnu.org
Subject: bug#19867: c++-mode indentation issues with C++1x initializer lists
Date: Sat, 14 Feb 2015 12:50:31 -0500 [thread overview]
Message-ID: <CAN1q39UD+_g9VQNCHKanbQvWt9mVWSHJQjZxsMC=VJ7JGG5wPQ@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 4142 bytes --]
Package: emacs
Version: 24.4.1
Severity: important
Related: c++-mode
Initializer lists use curly braces, but their contents do not indent
properly with emacs' c++-mode.
In short, one may use an initializer list to declare and initialize a
vector of integers as such:
std::vector<int> Foo( { 1, 2, 3, 4, 5 } );
Problems arise when the elements of the list span on multiple line and it
gets even worse when the elements are lambda-expressions and nested
initializer lists.
The following code illustrate most cases and related situations. The code
below compiles without error or warning with gcc 4.8.3.
In case email systems mess with the spaces, the code below is available at
this URL as well:
http://next.n32.ca/emacs_initlist_indentation_bug.txt
#include <vector>
#include <functional>
namespace emacs_initlist_indentation_bug {
struct ABC {
int a; // OK, text-book indentation
int b; //
int c; //
};
struct DEF {
int d, //
e, // indented from "int" + 2
f; //
};
struct GHI {
int //
g, // indented from "int" +0
h, // indented from "int" + 2
i; //
};
int f1 ( int a, // Indentation OK in function declaration context
int b, //
int c //
) // Notice how the ")" is indented
{
if(a>0){
return a+ // while out-of-topic, this probably pinpoints
b+ // what's going on internally
c; //
} else if(a<0) {
return (a+ // these are well aligned
b+ //
c); //
} else {
return
(a+ // these are well aligned
b+ //
c); //
}
}
void f2 (const ABC& abc)
{
f1(abc.a, // Indentation OK in function call context
abc.b, //
abc.c); //
}
void f3 (int a, int b, int c)
{
f1( f1( a+1, // Indentation OK, text-book example, perfect!
b+1, //
c+1 ), //
f1( a+2, //
b+2, //
c+2 ), //
f1( a+3, //
b+3, //
c+3 ) //
); //
}
void f4 (int a, int b, int c)
{
f2({a+1, // note "{" on same line as "("
b+1, // indented after "{" + 2
c+1} //
); // Bad! ")" indented underneath "(" instead of
argument (ie. "{")
f2(
{a+2, // note "{" on different line as "("
b+2, // indented after "{" + 3 !!!
c+2} //
); // OK, ")" indented underneath "{"
// Below are some typical indentation I'm getting these days.
// The only difference is in the newline on first line (and numerics)
std::vector<ABC> abcList1({{a+1, //
b+1, //
c+1}, //
{a+2, //
b+2, //
c+2}, //
{a+3, //
b+3, //
c+3} //
} //
); //
std::vector<ABC> abcList2( // Source of
alignment for closing ")" below
{{a+4, //
b+4, //
c+4}, //
{a+5, //
b+5, //
c+5}, //
{a+6, //
b+6, //
c+6} //
} //
); /* Somehow, this one aligns with first line's comment position!! */
}
void f5 ()
{
int foo = 0;
std::vector< std::function<int(int)> >
lambda_initlist_bug({ //
[foo](int x) //
{ // BAD, too indented by 2 positions
return x+x; //
}, //
[foo](int y) //
{ // BAD, too indented by 2 positions
return y+y; //
}, //
[foo](int z) //
{ // BAD, too indented by 2 positions
return z+z; //
} //
}
);
lambda_initlist_bug.push_back( //
[](int p) // OK, text-book indentation
{ //
return p+p; //
} //
); /* Aligned with first comment! */
for(auto f_lambda : lambda_initlist_bug){
f_lambda( 123 );
}
}
}
[-- Attachment #2: Type: text/html, Size: 5036 bytes --]
next reply other threads:[~2015-02-14 17:50 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-14 17:50 Simon [this message]
2015-02-16 19:09 ` bug#19867: c++-mode indentation issues with C++1x initializer lists Glenn Morris
2015-02-16 19:29 ` Simon
2015-02-16 21:38 ` Daniel Colascione
2015-02-17 19:23 ` Glenn Morris
2015-02-17 19:39 ` Simon
2015-02-16 21:03 ` Daniel Colascione
2020-11-23 14:14 ` Stefan Kangas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAN1q39UD+_g9VQNCHKanbQvWt9mVWSHJQjZxsMC=VJ7JGG5wPQ@mail.gmail.com' \
--to=turner25@gmail.com \
--cc=19867@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.