468,457 Members | 1,620 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

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 3881
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Hagen | last post: by
3 posts views Thread by kk_oop | last post: by
16 posts views Thread by Frederick Gotham | last post: by
2 posts views Thread by kgeorge2 | last post: by
9 posts views Thread by Slain | last post: by
1 post views Thread by Fokko Beekhof | last post: by
5 posts views Thread by zr | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.