开发者

How should I initialize a boost::array?

I am trying to understand the boost array. The code can be read easily from author's site.

In the design rationale, author (Nicolai M. Josuttis) mentioned that the following two types of initialization is possible.

boost::array<int,4> a = { { 1, 2, 3 } };  // Line 1
boost::array<int,4> a = { 1, 2, 3 };      // Line 2

In my experiment with g++ (version 4.1.2) Line 1 is working but Line 2 is not. (Line 2 yields the following:

warning: missing braces around initializer for 'int [4]'
warning: missing initializer for member 'boost::array<int, 4ul>::elems'

)

Nevertheless, my main question is, how Line 1 is working? I tried to write a class similar to array.hpp and use statement like Line 1, but that did not work :-(. The error is

typedef array< unsigned int, 10 > MyArray;

MyArray b = { { 1, 2, 3 } };  // Line 74

array_test.cpp:74: error: in C++98 'b' must be initialized by constructor, not by '{...}'
array_test.cpp:74: error: no matching f开发者_Python百科unction for call to 'array<unsigned int, 10u>::array(<brace-enclosed initializer list>)'
array.h:16: note: candidates are: array<unsigned int, 10u>::array()
array.h:16: note:                 array<unsigned int, 10u>::array(const array<unsigned int, 10u>&)

Can somebody explain me? Is there some boost specific thing happening in Line 1 that I need to be aware of?


this is a regular brace initialization list:

Boost array is defined like this:

struct array { T elems[N]; };

inner brace is for elems array initialization, outear brace is for struct initialization. If you provide your own constructor, you no longer have plain old datatype any cannot initialize using brace

notice that you can go without outer brace, but you will get a warning


The relevant section of the standard is §8.5.1, aggregates.

  1. An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or pro- tected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).
  2. When an aggregate is initialized the initializer can contain an initializer-clause consisting of a brace- enclosed, comma-separated list of initializer-clauses for the members of the aggregate, written in increasing subscript or member order. If the aggregate contains subaggregates, this rule applies recursively to the members of the subaggregate.

GCC 4.1.2 may be violating paragraph 11.

11 Braces can be elided in an initializer-list as follows. If the initializer-list begins with a left brace, then the succeeding comma-separated list of initializers initializes the members of a subaggregate; it is erroneous for there to be more initializers than members. If, however, the initializer-list for a subaggregate does not begin with a left brace, then only enough initializers from the list are taken to initialize the members of the subaggregate; any remaining initializers are left to initialize the next member of the aggregate of which the current subaggregate is a member.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜