Youtube - Lightning Talk Direct Aggregate Initialisation - Timur Doumler - CppCon 2021
Lightning Talk: Direct Aggregate Initialisation - Timur Doumler - CppCon 2021
https://youtu.be/flLNi0aejew
Transcrição

[00:09]
00:09 cool hi my name is timur i'm developer advocate at jetbrains a couple years ago i gave this talk uh where i was talking about all the different ways in which you can
[00:19]
00:19 initialize the variable and c plus plus of which there are a lot except that when i gave this talk it was before c plus plus 20 now we have sequence of 20 so now we have even more ways to
[00:28]
00:28 initialize a variable in c plus plus in particular we have this thing called well it's not actually called anything officially but i call it direct aggregate initialization because it's um
[00:37]
00:37 direct initialization which is then doing aggregate initialization what are we talking about we're talking about arrogance like for example an array so you could always initialize this like
[00:47]
00:47 this with a list initialization which is then going to perform aggregate initialization so list installation means you have these braces in the last 20 we can now instead of the braces use
[00:56]



00:56 parents which is direct initialization which is then going to do aggregation so that works since cpr says 20. you can do that not only with an array but also with
[01:05]
01:05 an aggregate class like the struct here with like two member uh data members like no us detect constructors so it's an aggregate you can install it with kerdis you can now
[01:15]
01:15 also initialize it with parents that works since you assess 20. why did the committee do this well it solves a few problems it solves this one problem where
[01:24]
01:24 you couldn't really perfect forward aggregates before c 20. that just wasn't possible now you can for example you can implement in place back so now you can in place
[01:33]
01:33 aggregates in cos20 that wasn't uh possible before the other thing you can now do which is a little bit more like rare i would say is you couldn't really do aggregate initialization inside a
[01:42]


01:42 macro because if you had inside the macro if you had curlies and then you had a comma in there the password would think that that's then the next macro argument after the comma and then the
[01:49]
01:49 puzzle will just explode now you can use parens so that actually compiles and now works seriously 20. so great yeah we introduced a new feature we solved two problems but you know with
[01:59]
01:59 cps plus there's like a hydra right like every time you chop off a head it goes to a new one so it's exactly the same here um so turns out when you do
[02:08]
02:08 curlys and when you parents it doesn't quite do the same thing it does aggregate initialization both of them but the second one
[02:17]
02:17 kind of treats its arguments the way world direct initialization does which is not the same way that list initialization works so we get a bunch of annoying differences for example
[02:27]



02:27 list initialization does not allow narrowing conversions directly integration does so this works with parents but not with curlies on the other hand if you have a brace collision
[02:38]
02:38 like you have a nested aggregate so you have an aggregate inside an aggregate well if you do curlys then you get brace solutions so you can basically do the flat right out the flat initializes and
[02:47]
02:47 it's going to recurse into the sub aggregates but prince that doesn't work well that's very unfortunate the kind of next one is
[02:58]
02:58 lifetime extension of references if you have a reference member so what list initialization is going to do if you do aggregatization via list initialization it's going to actually extend
[03:07]

03:07 the lifetime of the reference if you initialize it with a temporary um so that that works but if you do it with parents then it's not going to do that because direct integration is not
[03:15]
03:15 extending the lifetime of reference so then you're going to get a dangling reference and you're going to get a behavior and that is going to be very sad
[03:23]
03:23 um and finally there is this uh gem which uh pavel novikov sent me uh with a comment can you tell me what the hell is going on in this code so basically what's going on here is we have a struct
[03:34]
03:34 a which is an aggregate it has a member and then another member which is itself not in aggregate because it has a user-friend constructor and then we basically initialize this
[03:43]
03:43 aggregate with a list initialization by just initializing both of these uh members in there and that works with curly's that works with parens
[03:53]

03:53 what happens if you omit uh the second initializer you can do that right and it turns out if you do that with curlies you get a compiler error because if you omit the
[04:02]
04:02 initializer you get an implicitly initialized aggregate member and if you do that with curlies then what it's going to do is going to copy initialize that aggregate member as
[04:13]
04:13 if by copy initialization from an empty braced init list and copy initialization doesn't work with explicit constructors so you get a compiler error
[04:22]
04:22 if you do the same with parents you get the same compiler error which is fine but what happens if we omit then also the first initializer with the inch which doesn't really anything have
[04:32]
04:32 anything to do with this explicit thing right it's like it's just an end if you omit that okay great we get the same error but with parens we don't get the same error
[04:42]
04:42 what what's going on here well it turns out paramparan actually had a meaning before c plus plus 20. it actually had the meaning since c plus plus o3 it had the meaning of value initialization
[04:53]
04:53 which is going to do zero initialization which you know was valid before and we didn't break that um so it still has the same meaning as before so that's going to just zero out the the struct and
[05:04]
05:04 that's that's fine so basically yeah remember uh parent paren doesn't do aggregation very important and yeah thank you and that's the talk you