Discussion:
[dcl.init.aggr]/9 vs aggregates of aggregates of...
(too old to reply)
m***@gmail.com
2018-11-17 13:09:50 UTC
Permalink
Hello,

In the *[dcl.init.aggr]/9* (http://eel.is/c++draft/dcl.init.aggr#9) we can
An array of unknown bound initialized with a brace-enclosed
initializer-list containing n initializer-clauses, where n shall be greater
than zero, is defined as having n elements ([dcl.array]).
Is this correct for all cases?

Let's consider e.g. an array of scalars:
int x[] = { 0, 1, 2, 3 };
In this case it is correct. We have four initializer-clauses and size of
the `x` is four.

Now let's consider an array of aggregates with just one element:
struct int_wrapper
{
int value;
};
int_wrapper ints[] = { 0, 1, 2, 3 };
In this case it is correct, as well. We have four initializer-clauses and
size of the `ints` is four.

Now let's consider an array of aggregates with more than one elements:
struct point
{
int x, y;
};
point p[] = { 0, 1, 2, 3 };
In such case, *[dcl.init.aggr]/9* is not correct. We still have four
initializer-clauses but, because of *[dcl.init.aggr]/15* (
http://eel.is/c++draft/dcl.init.aggr#15) we end up with `p` as an array
with two elements.

Furthermore, this is recursive.
struct point { int x, y; };
struct bounds { point tl; point br; };
bounds b[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Ten initielizer-clauses, size of `b` is three.

I thought about wording of *[dcl.init.aggr]/9* that could fix this, but
topic is more complicated that it looks. There is a need to define how many
initializer-clauses can aggregate 'swallow'.
In other words, we need to know how many elements an aggregate has. If any
of these elements is an aggregate itself, we need to go recursive, and so
on.
*`aggregate-initializable-elements-count`* of an aggregate type is sum of
-one, if element is not an aggregate, otherwise
-its aggregate-initializable-elements-count [Example -
struct foo { int i; } // aggregate-initializable-elements-count is 1

struct bar { foo f1, f2; }; //
aggregate-initializable-elements-count is 2
struct baz { bar b1, b3; }; //
aggregate-initializable-elements-count is 4
- end example].
An array of unknown bound initialized with a brace-enclosed
initializer-list containing n initializer-clauses, where n shall be greater
-n elements, if array is of non-aggregate type, otherwise
-rounded up, n/aggregate-initializable-elements-count elements.
Please correct me if I messed up something.

Best,
Mateusz Janek

P.S.
About the names, I know that *aggregate-initializable-elements-count *could
be better named. If I'm right, and *[dcl.init.aggr]/9 *needs to be
corrected, we'll probably think of a better names.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Tam S. B.
2018-11-18 06:51:55 UTC
Permalink
It seems that the inference of array bound depends on the type of the initializers:

```
struct point
{
std::any x, y;
};

point p[] = { std::any(), std::any(), std::any(), std::any() }; // p has 2 elements

point q[] = { point(), point(), point(), point() }; // q has 4 elements
```

Maybe we could simply say the inference takes place after adding back the elided braces?

________________________________________
From: ***@gmail.com <***@gmail.com>
Sent: Saturday, November 17, 2018 1:09:50 PM
To: ISO C++ Standard - Discussion
Subject: [std-discussion] [dcl.init.aggr]/9 vs aggregates of aggregates of...

Hello,
An array of unknown bound initialized with a brace-enclosed initializer-list containing n initializer-clauses, where n shall be greater than zero, is defined as having n elements ([dcl.array]).
Is this correct for all cases?

Let's consider e.g. an array of scalars:
int x[] = { 0, 1, 2, 3 };
In this case it is correct. We have four initializer-clauses and size of the `x` is four.

Now let's consider an array of aggregates with just one element:
struct int_wrapper
{
int value;
};
int_wrapper ints[] = { 0, 1, 2, 3 };
In this case it is correct, as well. We have four initializer-clauses and size of the `ints` is four.

Now let's consider an array of aggregates with more than one elements:
struct point
{
int x, y;
};
point p[] = { 0, 1, 2, 3 };
In such case, [dcl.init.aggr]/9 is not correct. We still have four initializer-clauses but, because of [dcl.init.aggr]/15 (http://eel.is/c++draft/dcl.init.aggr#15) we end up with `p` as an array with two elements.

Furthermore, this is recursive.
struct point { int x, y; };
struct bounds { point tl; point br; };
bounds b[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Ten initielizer-clauses, size of `b` is three.

I thought about wording of [dcl.init.aggr]/9 that could fix this, but topic is more complicated that it looks. There is a need to define how many initializer-clauses can aggregate 'swallow'.
In other words, we need to know how many elements an aggregate has. If any of these elements is an aggregate itself, we need to go recursive, and so on.

For now I propose such wording:
Add one paragraph before [dcl.init.aggr]/9:
`aggregate-initializable-elements-count` of an aggregate type is sum of elements' count-values. Element's count-value is:
-one, if element is not an aggregate, otherwise
-its aggregate-initializable-elements-count [Example -
struct foo { int i; } // aggregate-initializable-elements-count is 1
struct bar { foo f1, f2; }; // aggregate-initializable-elements-count is 2
struct baz { bar b1, b3; }; // aggregate-initializable-elements-count is 4
- end example].

Change [dcl.init.aggr]/9 to:
An array of unknown bound initialized with a brace-enclosed initializer-list containing n initializer-clauses, where n shall be greater than zero, is defined as having:
-n elements, if array is of non-aggregate type, otherwise
-rounded up, n/aggregate-initializable-elements-count elements.


Please correct me if I messed up something.

Best,
Mateusz Janek

P.S.
About the names, I know that aggregate-initializable-elements-count could be better named. If I'm right, and [dcl.init.aggr]/9 needs to be corrected, we'll probably think of a better names.



--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org<mailto:std-discussion+***@isocpp.org>.
To post to this group, send email to std-***@isocpp.org<mailto:std-***@isocpp.org>.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Richard Smith
2018-11-18 18:45:32 UTC
Permalink
Post by m***@gmail.com
```
struct point
{
std::any x, y;
};
point p[] = { std::any(), std::any(), std::any(), std::any() }; // p has 2 elements
point q[] = { point(), point(), point(), point() }; // q has 4 elements
```
Maybe we could simply say the inference takes place after adding back the elided braces?
That is the way the wording is intended to be interpreted already.

If you don't assume that http://eel.is/c++draft/dcl.init.aggr#15 happens
before all the other aggregate initialization rules, nothing about it makes
any sense; this is not limited to array bound inference.

________________________________________
Post by m***@gmail.com
Sent: Saturday, November 17, 2018 1:09:50 PM
To: ISO C++ Standard - Discussion
Subject: [std-discussion] [dcl.init.aggr]/9 vs aggregates of aggregates of...
Hello,
An array of unknown bound initialized with a brace-enclosed
initializer-list containing n initializer-clauses, where n shall be greater
than zero, is defined as having n elements ([dcl.array]).
Is this correct for all cases?
int x[] = { 0, 1, 2, 3 };
In this case it is correct. We have four initializer-clauses and size of the `x` is four.
struct int_wrapper
{
int value;
};
int_wrapper ints[] = { 0, 1, 2, 3 };
In this case it is correct, as well. We have four initializer-clauses and
size of the `ints` is four.
struct point
{
int x, y;
};
point p[] = { 0, 1, 2, 3 };
In such case, [dcl.init.aggr]/9 is not correct. We still have four
initializer-clauses but, because of [dcl.init.aggr]/15 (
http://eel.is/c++draft/dcl.init.aggr#15) we end up with `p` as an array
with two elements.
Furthermore, this is recursive.
struct point { int x, y; };
struct bounds { point tl; point br; };
bounds b[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Ten initielizer-clauses, size of `b` is three.
I thought about wording of [dcl.init.aggr]/9 that could fix this, but
topic is more complicated that it looks. There is a need to define how many
initializer-clauses can aggregate 'swallow'.
In other words, we need to know how many elements an aggregate has. If
any of these elements is an aggregate itself, we need to go recursive, and
so on.
`aggregate-initializable-elements-count` of an aggregate type is sum of
-one, if element is not an aggregate, otherwise
-its aggregate-initializable-elements-count [Example -
struct foo { int i; } // aggregate-initializable-elements-count is 1
struct bar { foo f1, f2; }; //
aggregate-initializable-elements-count is 2
struct baz { bar b1, b3; }; //
aggregate-initializable-elements-count is 4
- end example].
An array of unknown bound initialized with a brace-enclosed
initializer-list containing n initializer-clauses, where n shall be greater
-n elements, if array is of non-aggregate type, otherwise
-rounded up, n/aggregate-initializable-elements-count elements.
Please correct me if I messed up something.
Best,
Mateusz Janek
P.S.
About the names, I know that aggregate-initializable-elements-count could
be better named. If I'm right, and [dcl.init.aggr]/9 needs to be corrected,
we'll probably think of a better names.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Continue reading on narkive:
Loading...