* If it's alright with you, I'd like to include you in the list of references. If you'd like to be included, any particular way? Link to your account here, email address, webpage, etc?
The same goes for anyone else who has contributed.
— if _RangeT is an array type, begin-expr and end-expr are __range and __range + __bound, respec-
tively, where __bound is the array bound. If _RangeT is an array of unknown size or an array of
incomplete type, the program is ill-formed;
Would it be safe to assume that "__bound = (sizeof(__range) / sizeof(decltype(*__begin))"
thus forming the following synthetic code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int array[] = {1, 2, 3, 4};
for(int i : array)
{
// do something
}
// is equal to
auto&& __range = (array);
for(auto __begin = __range, __end = __range + (sizeof(__range) / sizeof(decltype(*__begin))); __begin != __end; ++__begin)
{
auto i = *__begin;
// do something
}
Yes, however what __bound be described as deduced from? The standard left that out. What I am trying to do is provide synthetic(ish) output of a range-based for loop over an array based on how the standard describes it. I find it very odd that __bound isn't described better.
__bound = (sizeof(__range) / sizeof(decltype(*__begin))); certainly seems to be well-formed and does work correctly on my system, but would it be safe to assume that is how the synthetic code would deduce it? Perhaps the standard doesn't provide enough information in this context.
> what __bound be described as? The standard left that out.
Did it? It says:
... where __bound is the array bound ...
[8.3.4 Arrays]... The constant expression specifies the bound of (number of elements in) the array. If the value of the constant expression is N, the array has N elements numbered 0 to N-1 ...
It would seem your answer is as general as the standard is in this regard.
Perhaps I need to rephrase my question:
Where __bound meets the requirements ( complete type and statically allocated) __bound can be calculated as sizeof(array) / sizeof(decltype(*__begin), correct?
The standard provides no synthetic example of a range-based for loop with a static array. Knowing that the standard defines an array bound to be 'N' doesn't help me in this situation, as there is no 'N' constant expression in the example found in §6.5.4.
Simply put, would I be putting words in the standard's mouth if I state that N == (sizeof(array) / sizeof(decltype(*__begin)) && __bound == N in the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int array[] = {1, 2, 3, 4};
for(int i : array)
{
// do something
}
// is equal to
auto&& __range = (array);
for(auto __begin = __range, __end = __range + (sizeof(__range) / sizeof(decltype(*__begin))); __begin != __end; ++__begin)
{
auto i = *__begin;
// do something
}
?
Thanks for being patient with me. I just want to get the facts 110% straight.
If we have int array[] = {1, 2, 3, 4};, the expression sizeof(array) implies that the complete type of array is known; the complete type of an array is known if and only if the bound of the array is already known.
In the above example, type of array is int[4], and sizeof(array) == sizeof( int[4] ) == sizeof(int) * 4
That the bound of the above array is known to be 4 comes from:
An array bound may also be omitted when the declarator is followed by an initializer. In this case the bound is calculated from the number of initial elements (say, N) supplied - IS
I understand how sizeof works, but was only wondering how __bound was most likely to represented in the example found in the standard. Conceptually, yes, it is the bound of the array. This can be found in numerous ways.
1 2 3
int array[] = {1, 2, 3, 4};
//__bound = sizeof(array) == sizeof(int[4] == (sizeof(int) * 4) == (sizeof(__range) / sizeof(decltype(*__begin))
// which version of the same thing that appears in the synthetic code seems to be unspecified
At this point I'll assume that it is unspecified, since I'm sure you would have given me a direct answer by now otherwise.