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

A quick C++11 initializer list question (for set)

48 views
Skip to first unread message

JiiPee

unread,
Aug 19, 2015, 4:50:08 PM8/19/15
to
I have all these books and read most of it, but this one cannot find on
what page it is. So maybe somebody can help.

I have this struct:
struct Coordinate
{
int x; // row
int y; // column
};

and then if I do:
Coordinate c;
c = {2,7};

it correctly and as expected sets x and y to 2,7 for c. But am
interested to know what happens here exactly. My guess is that it
creates a temporary Coordinate object like this:

Coordinate temp{2,7};
and then does a copy:
c = temp;
Because Coordinate does not have a inilizer list constructor and nor a
normal constructor Coordinate(int, int) it just sets the members to 2
and 7 in that order. I am right?

red floyd

unread,
Aug 19, 2015, 5:14:37 PM8/19/15
to
Isn't this just C-style struct initialization?

Melzzzzz

unread,
Aug 19, 2015, 5:21:21 PM8/19/15
to
It looks like assignment.

JiiPee

unread,
Aug 19, 2015, 5:50:06 PM8/19/15
to
On 19/08/2015 22:14, red floyd wrote:
>
> Isn't this just C-style struct initialization?
>

I don't think it is (its not an initialization, its an assignment).

And for example:

class Coordinate2
{
int x; // row
int y; // column
public:
Coordinate2(int j, int l) {}
};

Coordinate2 o{4,4};
o = { 3,4 };

this works. But if you take out the constructor Coordinate2(int j, int
l) {} then it does not compile. So it cannot do it for private members,
but if x is public then works without a constructor.

Ben Bacarisse

unread,
Aug 19, 2015, 6:17:07 PM8/19/15
to
It's not (as someone suggested) a C assignment[1]. The {2, 7} is an
initializer list, even though what you've written is an assignment. So
what it is initialising? You assignment is, in fact, a call to a
compiler-defined assignment operator

Coordinate &Coordiante::operator=(const Coordinate &other);

and the {2, 7} is being used to initialize the single parameter. The
supplied opertaor simple does a member-wise assignment using = for each
of the data members. Initializer lists (in this form) are a C++11
feature.

[1] C99 has compound literals. Using them you'd write the above as

c = (struct Coordinate){2, 7};

but C++ does not have compound literals.

--
Ben.

JiiPee

unread,
Aug 19, 2015, 8:19:29 PM8/19/15
to
ok but to be able to use the operator= the compiler must create a
temporary object (like "other") out of {2, 7} , isnt it?
So for example:

Coordinate other{2, 7};
c = other;
(this is done somewhere temporary)

I mean it will create a temporary Coordinate object using {2, 7}?

Paavo Helde

unread,
Aug 20, 2015, 3:52:11 AM8/20/15
to
JiiPee <n...@notvalid.com> wrote in news:829Bx.467861$ix5.1...@fx17.am4:

> On 19/08/2015 23:16, Ben Bacarisse wrote:
>> JiiPee <n...@notvalid.com> writes:
>>> struct Coordinate
>>> {
>>> int x; // row
>>> int y; // column
>>> };
>>>
>>> Coordinate c;
>>> c = {2,7};
>> You assignment is, in fact, a call to a
>> compiler-defined assignment operator
>>
>> Coordinate &Coordiante::operator=(const Coordinate &other);
>>
>> and the {2, 7} is being used to initialize the single parameter. The
>> supplied opertaor simple does a member-wise assignment using = for
each
>> of the data members.
>
> ok but to be able to use the operator= the compiler must create a
> temporary object (like "other") out of {2, 7} , isnt it?

Yes, at least conceptually a temporary is created which is then copied to
c by using the copy assignment operator. If anything like the copy
assignment is identifiable in the compiled machine code is another
question, aggregates are pretty transparent to the compiler and can be
optimized heavily (even though copy assignment cannot be elided so easily
as copy constructor, AFAIK).

Bo Persson

unread,
Aug 20, 2015, 11:22:38 AM8/20/15
to
In theory, yes. In practice, if the compiler cannot optimize the
assignment of two ints, I will get another compiler.


Bo Persson


red floyd

unread,
Aug 20, 2015, 1:48:02 PM8/20/15
to
Oops. You and JiiPee are correct. I misread it as
Coordinate c = { 2, 7 };


0 new messages