Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 Uniform Initialization Formatted Incorrectly by Eclipse

I'm trying to use C++11 uniform initialisation in Eclipse CDT Luna 4.4 so that I can spot where narrowing is taking place more easily. I have two issues. The first is that the code style formatter is treating the brace initialisation as if it were a block of code at times (example #2). The second being, uniform initialisation without the assignment operator gives me both a compiler error and a warning (example #4).

My project is set up to use the "ISO C++11 (-std=c++0x)" language standard dialect. Using gcc version 4.8.1 (GCC).

Here are some formatting examples each containing before and after code:

Example #1 - initialisation - Formats fine, no compiler messages:

int x = {9};

int x = {9};

Example #2 - Assignment - The formatter's option for 'Blocks' on the 'Braces' tab controls 'if' statement styling, it also influences the below assignment behaviour when 'Blocks' is set to 'Next line'. I wish to continue using the 'Next line' brace style (Allman) for my 'if' statements instead of setting it to 'Same line' (K&R) which would also stop the new line behaviour observed here:

int x2;
x2 = {9};

int x2;
x2 =
{       9};

Example #3 - Initialisation without assignment operator - Formats fine, no compiler messages:

int y{9};

int y{9};

Example #4 - Assignment without assignment operator - Formats fine, however gives a expected ';' before '{' token error and a statement has no effect [-Wunused-value] compiler warning:

int y2;
y2{9};

int y2;
y2{9};

There seems to be no middle ground. Using uniform initialisation to initialise works fine in both examples #1 and #3 but when it comes to assignment in #2 and #4, the former has formatting errors and the latter produces compiler messages.

I am a C++ novice, anyone have any suggestions or see the elephant in the room in my logic? I am getting the impression somehow that uniform initialisation should not be used for assignment as I do not see any examples of this but what's confusing me is that even though assigning in #2 gives formatting errors, the compiler does give the correct narrowing conversion ... [-Wnarrowing] compiler warning when fed a floating-point number.

Any help is appreciated, thank you!

like image 662
user3726423 Avatar asked Oct 01 '22 06:10

user3726423


2 Answers

I'll try to explain from The C++ Programming Language, 4th Edition, the more detailed explanation can be found at section 11.3.

{}-lists can be used for initializing named variable (example #1 and #3). In addition, {}-lists can be used as expressions and they can appear in two forms.

  • Qualified Lists, T{...} that creates an object of type T
  • Unqualified Lists, {...} that the type must be determined from the context of use.

Qualified Lists: If you can initialise a variable x as T x {v} then you can create object as an expression using T{v} or new T{v}.

Unqualified Lists: It can be used where an expected type is unambiguously known.

  • The right-hand operand of an assignment operator like =, +=, -= etc. (example #2)
  • Function argument
  • Return value
  • Subscript

Example:

int x1{9};         // direct initialisation

int x2 = {9};      // copy initialisation

int x3; 
x3 = {9};          // right-hand operand of assignment operator
x3 += {1};         // right-hand operand of assignment operator
x3 = 9 + {1}       // error, only right-hand operand of assignment operator

So, example #1 and #3 are named variable initialisations, example #2 is right-hand operand of an assignment operator (it can be reported as bug to Eclipse) and example #4 is not valid syntax.

like image 116
Alper Avatar answered Oct 11 '22 15:10

Alper


I'm certain that your example #2 is a bug in Eclipse CDT; it's a useful tool, but it's not a full-blown C++ compiler, and it sometimes gets things wrong. If Eclipse CDT doesn't understand something that the C++ compiler does, then it's a bug in Eclipse and should be reported on the Eclipse Bugzilla.

However, using a uniform initializer for assignment looks weird to me. I'm sure some of it is my relative lack of experience with C++11, but why not stick with a more conventional coding style and use other compiler flags (like -Wconversion) to catch unwanted loss of precision?

like image 29
Josh Kelley Avatar answered Oct 11 '22 15:10

Josh Kelley