Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What does this do ???

64 views
Skip to first unread message

Bob Langelaan

unread,
Mar 15, 2016, 5:06:11 PM3/15/16
to
This code compiles on MS VS 2015:

#include <array>
int main()
{
std::array<bool, 100> bArray;
bArray = { false };
}

What does the last line of code do?

Thanks,
Bob

Vir Campestris

unread,
Mar 15, 2016, 5:16:18 PM3/15/16
to
Initialises the first element of the array with false.

http://en.cppreference.com/w/cpp/container/array

and follow the links to

http://en.cppreference.com/w/cpp/language/aggregate_initialization

HTH
Andy

Marcel Mueller

unread,
Mar 15, 2016, 5:17:14 PM3/15/16
to
Assign a list of one boolean to the array. Due to the fixed nature of
std::array this will in fact clear all elements.


Marcel

Bob Langelaan

unread,
Mar 15, 2016, 5:23:12 PM3/15/16
to
Marcel,

Are you saying that the last statement assigns all 100 bool values in bArray to false? I would have thought a loop of some type would have been necessary to accomplish this? Can you expand on the last statement you made? Thanks,
Bob

Bob Langelaan

unread,
Mar 15, 2016, 5:36:59 PM3/15/16
to
Ok, I modified code to:

#include <array>
#include <iostream>

int main()
{
std::array<bool, 100> bArray;
bArray = { true };
for (auto x : bArray)
{
std::cout << x << " ";
}
}

And I have figured out what is happening by varying whether true or false is assigned or statement is commented out.

If assigned false, all 100 bools become false; if assigned true, only the first element becomes true, the remainder become false; if commented out the 100 bools have seemingly random values (memory in bArray is not initialized in other words).

Marcel Mueller

unread,
Mar 15, 2016, 5:49:14 PM3/15/16
to
On 15.03.16 22.22, Bob Langelaan wrote:
>> Assign a list of one boolean to the array. Due to the fixed nature of
>> std::array this will in fact clear all elements.
>>
> Are you saying that the last statement assigns all 100 bool values in bArray to false?

Exactly.

> I would have thought a loop of some type would have been necessary to accomplish this? Can you expand on the last statement you made? Thanks,

It is a ->"list initializer".

And similar to C array assignments trailing elements are initialized to
zero i.e. false.


Marcel

Öö Tiib

unread,
Mar 16, 2016, 3:24:14 AM3/16/16
to
Yes, it is aggregate so when initialized it will be default-initialized
(so falses) to end and when not initialized then it will be uninitialized.

Bob Langelaan

unread,
Mar 16, 2016, 1:23:51 PM3/16/16
to
But in my mind an entity can only be "initialized" when it is defined. Therefore the statement:

bArray = { false };

does not initialize bArray because bArray was previously defined.

I am assuming that what is happening that the { false } value on the right of the assignment statement is being converted to a temporary std::array<bool, 100> object which is initialized to { false }. This temporary object is then assigned to bArray.

Is my assumption correct?

Thanks,
Bob

Öö Tiib

unread,
Mar 16, 2016, 7:04:00 PM3/16/16
to
Yes, perhaps bad wording of mine.

> Therefore the statement:
>
> bArray = { false };
>
> does not initialize bArray because bArray was previously defined.
>
> I am assuming that what is happening that the { false } value on the right of the assignment statement is being converted to a temporary std::array<bool, 100> object which is initialized to { false }. This temporary object is then assigned to bArray.
>
> Is my assumption correct?

Yes, the '{ false }' is of magic C++11 type 'std::initializer_list'.
Converted from it is a temporary object of type 'std::array<bool, 100>'
The "conversion" is same as initialization as aggregate and then it
is assigned to 'bArray'. Actually compiler likely optimizes all the
temporaries out.

Bob Langelaan

unread,
Mar 16, 2016, 7:14:15 PM3/16/16
to
Thanks! I think I have it now :)
0 new messages