473,375 Members | 1,299 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,375 software developers and data experts.

tr1::array initializater syntax

The latest GCC (4.2.3) gives a warning if a tr1::array is initialized
with the traditional syntax:

#include <iostream>
#include <tr1/array>

int main() {
std::tr1::array<int, 35a = { 0 };
std::cout << a[4] << '\n';
}

g++ -ansi -pedantic -Wall main.cc -o main
main.cc: In function 'int main()':
main.cc:5: warning: missing braces around initializer for 'int [35]'

Is this the expected behavior? What's the short, static way to
initialize a tr1::array of arbitrary size?
Feb 14 '08 #1
7 4109
Jeff Schwab wrote:
The latest GCC (4.2.3) gives a warning if a tr1::array is initialized
with the traditional syntax:

#include <iostream>
#include <tr1/array>

int main() {
std::tr1::array<int, 35a = { 0 };
std::cout << a[4] << '\n';
}

g++ -ansi -pedantic -Wall main.cc -o main
main.cc: In function 'int main()':
main.cc:5: warning: missing braces around initializer for 'int
[35]'
Is this the expected behavior? What's the short, static way to
initialize a tr1::array of arbitrary size?
Since 'std::tr1::array' has the actual array as the 'elems' member,
you need to take your zero in curly braces:

std::tr1::array<int, 35a = { {0} };

That's what the compiler is telling you, BTW.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 14 '08 #2
On 2008-02-14 16:00:20 -0500, Jeff Schwab <je**@schwabcenter.comsaid:
The latest GCC (4.2.3) gives a warning if a tr1::array is initialized
with the traditional syntax:

#include <iostream>
#include <tr1/array>

int main() {
std::tr1::array<int, 35a = { 0 };
std::cout << a[4] << '\n';
}

g++ -ansi -pedantic -Wall main.cc -o main
main.cc: In function 'int main()':
main.cc:5: warning: missing braces around initializer for 'int [35]'

Is this the expected behavior? What's the short, static way to
initialize a tr1::array of arbitrary size?
The compiler is telling you that you might not know what you're doing,
despite the fact that this initialization is legal and its meaning is
well defined. If you agree with the compiler writer's stylistic
judgment, change the code. Personally, I don't think any compiler
writer knows enough about the code that I'm writing to tell me how I
should write it. Turn off the warning.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Feb 14 '08 #3
On 2008-02-14 16:16:27 -0500, "Victor Bazarov" <v.********@comAcast.netsaid:
Jeff Schwab wrote:
>The latest GCC (4.2.3) gives a warning if a tr1::array is initialized
with the traditional syntax:

#include <iostream>
#include <tr1/array>

int main() {
std::tr1::array<int, 35a = { 0 };
std::cout << a[4] << '\n';
}

g++ -ansi -pedantic -Wall main.cc -o main
main.cc: In function 'int main()':
main.cc:5: warning: missing braces around initializer for 'int
[35]'
Is this the expected behavior? What's the short, static way to
initialize a tr1::array of arbitrary size?

Since 'std::tr1::array' has the actual array as the 'elems' member,
you need to take your zero in curly braces:

std::tr1::array<int, 35a = { {0} };

That's what the compiler is telling you, BTW.
Well, it's not saying that you "need to" put in the unnecessary braces.
All it's doing is whining about some compiler writer's notion of good
style.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Feb 14 '08 #4
Pete Becker wrote:
On 2008-02-14 16:16:27 -0500, "Victor Bazarov" <v.********@comAcast.net>
said:
>Jeff Schwab wrote:
>>The latest GCC (4.2.3) gives a warning if a tr1::array is initialized
with the traditional syntax:

#include <iostream>
#include <tr1/array>

int main() {
std::tr1::array<int, 35a = { 0 };
std::cout << a[4] << '\n';
}

g++ -ansi -pedantic -Wall main.cc -o main
main.cc: In function 'int main()':
main.cc:5: warning: missing braces around initializer for 'int
[35]'
Is this the expected behavior? What's the short, static way to
initialize a tr1::array of arbitrary size?

Since 'std::tr1::array' has the actual array as the 'elems' member,
you need to take your zero in curly braces:

std::tr1::array<int, 35a = { {0} };

That's what the compiler is telling you, BTW.
Thank you. I should have looked more carefully before posting.
Well, it's not saying that you "need to" put in the unnecessary braces.
All it's doing is whining about some compiler writer's notion of good
style.
Then standard C++ does *not* require the double braces? If not, I'd
rather not use them.

What surprises me is that I had vaguely expected to be able to change
array types by changing a typedef in a library header. If I don't want
clients to see warnings, though, it looks like any such initializations
have to be changed in the client code:

#include <iostream>
#include <tr1/array>

struct my_lib {

/* Use tr1::array as a drop-in replacement for raw array. */
// typedef int array_type[32];
typedef std::tr1::array<int, 32array_type;
};

int main() {

/* Oops! Brace-related warning from some compilers. */
my_lib::array_type an_array = { 0 };

(void)an_array;
}

This doesn't actually bite me too hard, since I rarely expose raw arrays
like that anyway, but I imagine some folks will have a problem with it.
It sounds like this is a QoI issue, though, not a language or library
issue; is that correct?
Feb 15 '08 #5
On 2008-02-14 20:37:32 -0500, Jeff Schwab <je**@schwabcenter.comsaid:
>
Then standard C++ does *not* require the double braces? If not, I'd
rather not use them.
Aggregate initialization just goes element by element. You can also
have parentheses around the initializer for an element, but it's not
required.

Here's a more elaborate example:

struct S
{
int i, j;
};

In all the following array definitions, pretend that the definition
occurs inside a function.

S s[2] = { 1, 2, 3 };

This initializes s[0].i to 1, s[0].j to 2, s[1].i to 3, and s[1].j to
0. The fully-bracketed form (which is probably preferable here) would
be:

S s[2] = { { 1, 2 }, { 3, 0 } };

On the other hand, all-zeros initialization is far more obvious, so I
usually go with this:

S s[2] = { 0 };

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Feb 15 '08 #6
On 2008-02-14 21:46:37 -0500, Pete Becker <pe**@versatilecoding.comsaid:
On 2008-02-14 20:37:32 -0500, Jeff Schwab <je**@schwabcenter.comsaid:
>>
Then standard C++ does *not* require the double braces? If not, I'd
rather not use them.

Aggregate initialization just goes element by element. You can also
have parentheses around the initializer for an element, but it's not
required.

Here's a more elaborate example:

struct S
{
int i, j;
};
And here's a wierd one I forgot:

S s[2] = { {1}, 2, 3 };

This initializes s[0].i to 1, s[0].j to 0, s[1].i to 2, and s[1].j to 3.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Feb 15 '08 #7
Pete Becker wrote:
On 2008-02-14 21:46:37 -0500, Pete Becker <pe**@versatilecoding.comsaid:
>On 2008-02-14 20:37:32 -0500, Jeff Schwab <je**@schwabcenter.comsaid:
>>>
Then standard C++ does *not* require the double braces? If not, I'd
rather not use them.

Aggregate initialization just goes element by element. You can also
have parentheses around the initializer for an element, but it's not
required.

Here's a more elaborate example:

struct S
{
int i, j;
};

And here's a wierd one I forgot:

S s[2] = { {1}, 2, 3 };

This initializes s[0].i to 1, s[0].j to 0, s[1].i to 2, and s[1].j to 3.
That is fascinating. I'm always amazed at how much I don't know about
this language. Thanks for the explanation.
Feb 15 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hagen | last post by:
Hi, I have a question that you probably shouldnĀ“t worry about since the compiler cares for it, but anyways: When you run your compiler with optimization turned on (eg. g++ with -Ox flag) and...
3
by: kk_oop | last post by:
Hi. I recently wrote a simple little template that defines an array that checks attempts to use out of bounds indexes. The only problem is that it does provide the use array style value...
16
by: Frederick Gotham | last post by:
Inspired by page 219 of Nicolai M. Josuttis's book, I set out to write a class for an intrinsic array which would behave, to as far an extent as possible, like a container. Also though, I wanted no...
2
by: kgeorge2 | last post by:
A library which i use, has an array class class LibArray { public: LibArray():_array(NULL),_len(0); LibArray(int n, float val):_array( new float),_len(n){} /** more complete definition...
13
by: stephen b | last post by:
(apologies for cross posting from the moderated group..i'm sure you understand) Hello, I'm passing an array into a Constructor and hoping to use it as a pointer and store it as a class member...
9
by: Slain | last post by:
I have more of a conceptual question now. Let us say I do this:- char *str; --create an array of pointers str= "John"; I thought this would automatically put John at some memory space and...
1
by: Fokko Beekhof | last post by:
Hello, The following code compiles (gcc/linux/gnu platform) ////////////////////// #include <tr1/array> int main() { std::tr1::array<int, 7a; std::tr1::array<int, 6>::iterator i =...
5
by: zr | last post by:
Hi, Is there a way to initialize a std::tr1::array with a pre-allocated built-in array in a copy-less assignment, such that both will point to the same memory? Vice-versa is easy to do, simply...
0
by: zr | last post by:
On Nov 10, 6:35 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com> wrote: I had to write a small test to realize the problem, though i don't understand what is really happening. Max, or anyone...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.