Can you tell me how to initialise the new style C++ arrays please.
I am trying :
array<int, 12> monthdays (29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
but it doesn't appear to work!
Can you tell me how to initialise the new style C++ arrays please.
I am trying :
array<int, 12> monthdays (29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
but it doesn't appear to work!
First you have 13 parameters so your array should be size 13 not 12. Second you need to use braces not the ().
Code:array<int, 13> monthdays = {{29, 31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31}};
Jim
You can simplify that slightly to:
Of course, guessing by the name of the array, your initialiser data is not correctCode:array<int, 13> monthdays = {29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks Jim
Checking the grammar, I'd say that's a compiler bug:Originally Posted by jimblumberg
Now, an initializer-list consists of zero or more comma-separated initializer-clauses. An initializer-clause may consist of an assignment-expression. An assignment-expression may consist of an integer constant (integer literal). Therefore, my example is valid, so it was correct for this bug to be fixed in g++ 4.6.Originally Posted by C++11 Clause 23.3.2.1 Paragraph 2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Okay, that's what I thought. So I guess it's time to upgrade compilers again.
Jim
I suspect that it's because it thinks you're initializing the internal array of the class, so the outer set of braces is for the class initializer, and the inner set is for the array.
It looks like there is an open bug report about this issue. See this bug report.
Jim
Well spotted Jim!
I assume then that it is ok just to use 1 set of braces and ignore the warning - or would you recommend using two sets so that there is no warning?
Since, at the moment, I am not writing programs for anyone but myself I would probably use the two braces to quiet the compiler. However if I were going to distribute a program I would probably not use the initialization list at all, until the bug was resolved. I would manually initialize each element.
Jim