An example is the Point structure, which you want to store in a C-format array.
If you define each element in parentheses as shown below, there will be no compilation error, but
Output does not meet expectations.
Only 5 elements will be printed, and the order will be unknown.
By the way, when I used vector, I got a compilation error.
#include<iostream>
# include <vector>
US>structure Point {
int32_tx;
int32_ty;
};
int main()
{
Point points [ ] {
(-1, -1), ( 0, -1), ( 1, -1),
(-1, 0), ( 0, 0), ( 1, 0),
(-1, 1), ( 0, 1), ( 1, 1),
};
for (auto i=std::begin(points), e=std::end(points); i!=e;++i)
{
printf("(%2d, %2d)\n", i->x, i->y);
}
/*
std::vector<Point>points {//compilation error
(-1, -1), (0, -1), (1, -1),
(-1, 0), (0, 0), (1, 0),
(-1, 1), (0, 1), (1, 1),
};
*/
return 0;
}
(-1,-1)
(-1, 0)
( 0, 0)
( 1, 1)
( 1, 0)
By initializing each element in parentheses as shown below, nine elements will be printed as expected.
Point points[]{
{-1, -1}, { 0, -1}, { 1, -1},
{-1, 0}, { 0, 0}, { 1, 0},
{-1, 1}, { 0, 1}, { 1, 1},
};
(-1,-1)
( 0,-1)
( 1,-1)
(-1, 0)
( 0, 0)
( 1, 0)
(-1, 1)
( 0, 1)
( 1, 1)
As I mentioned earlier, what happens when each element is enclosed in parentheses?
I do not understand, so please let me know.
Why does the compiler recognize that there are five elements?
[Operating Environment]
Microsoft Visual Studio Community 2022
Version 17.4.4
VisualStudio.17.Release/17.4.4+33213.308
[Platform Toolset Visual Studio 2022(v143)
[C++ Language Standard] ISO C++ 17 Standard
[C Language Standard] Default (Traditional MSVC)
(x,y)
is interpreted as prioritizing the x,y
expression x,y
with the comma operator in parentheses, so the initialization of the presentation points
is only valid
Point points[]{
(-1, -1), (0, -1), (1, -1), // are read as -1, -1, -1.
(-1, 0), (0, 0), (1, 0), // are read as 0, 0, 0
(-1,1), (0,1), (1,1), // are read as 1, 1, 1.
};
Rewrite
Point points[]{
-1, -1,
-1, 0,
0, 0,
1, 1,
1
};
Each element requires two initializers, but there are only nine initializers on the code, so the final element is supplemented with the initializer 0
to result in the presentation.
Compiled in this environment (g++) the following warning appears:
qq2.cc:In function 'int main()':
qq2.cc:12:10:warning:left operator of communication has no effect [-Wunused-value]
12 | (-1, -1), ( 0, -1), ( 1, -1),
| ^~
qq2.cc:12:21:warning:left operator of communication has no effect [-Wunused-value]
12 | (-1, -1), ( 0, -1), ( 1, -1),
| ^
qq2.cc:12:31:warning:left operator of communication has no effect [-Wunused-value]
12 | (-1, -1), ( 0, -1), ( 1, -1),
| ^
qq2.cc:13:10:warning:left operator of communication has no effect [-Wunused-value]
13 | (-1, 0), ( 0, 0), ( 1, 0),
| ^~
qq2.cc:13:21:warning:left operator of communication has no effect [-Wunused-value]
13 | (-1, 0), ( 0, 0), ( 1, 0),
| ^
qq2.cc:13:31:warning:left operator of communication has no effect [-Wunused-value]
13 | (-1, 0), ( 0, 0), ( 1, 0),
| ^
qq2.cc:14:10:warning:left operator of communication has no effect [-Wunused-value]
14 | (-1, 1), ( 0, 1), ( 1, 1),
| ^~
qq2.cc:14:21:warning:left operator of communication has no effect [-Wunused-value]
14 | (-1, 1), ( 0, 1), ( 1, 1),
| ^
qq2.cc:14:31:warning:left operator of communication has no effect [-Wunused-value]
14 | (-1, 1), ( 0, 1), ( 1, 1),
In Visual Studio, if you set the warning level to /Wall
ConsoleApplication 3.cpp(11,11): warning C4548: expression before common has no effect; expected expression with side-effect
ConsoleApplication 3.cpp(11,20): warning C4548: expression before comma has no effect; expected expression with side-effect
ConsoleApplication 3.cpp(11,29): warning C4548: expression before comma has no effect; expected expression with side-effect
ConsoleApplication 3.cpp(12,11): warning C4548: expression before comma has no effect; expected expression with side-effect
ConsoleApplication 3.cpp(12,20): warning C4548: expression before comma has no effect; expected expression with side-effect
ConsoleApplication 3.cpp(12,29): warning C4548: expression before comma has no effect; expected expression with side-effect
ConsoleApplication 3.cpp(13,11): warning C4548: expression before comma has no effect; expected expression with side-effect
ConsoleApplication 3.cpp(13,20): warning C4548: expression before comma has no effect; expected expression with side-effect
ConsoleApplication 3.cpp(13,29): warning C4548: expression before comma has no effect; expected expression with side-effect
ConsoleApplication 3.cpp(11,12): warning C5246: 'points': the initialization of a subobject should be wrapped in braces
ConsoleApplication 3.cpp(11,30): warning C5246: 'points': the initialization of a subobject should be wrapped in braces
ConsoleApplication 3.cpp(12,21): warning C5246: 'points': the initialization of a subobject should be wrapped in braces
ConsoleApplication 3.cpp(13,12): warning C5246: 'points': the initialization of a subobject should be wrapped in braces
ConsoleApplication 3.cpp(13,30): warning C5246: 'points': the initialization of a subobject should be wrapped in braces
will be alerted.Each warning is
That's right, but I just checked after receiving the answers from 774RR and akiraejiri, and I thought it would be difficult to expect this warning.
© 2024 OneMinuteCode. All rights reserved.