Initializing member variables
Should we do: struct Foo { int bar = 0; } Or struct Foo { int bar { 0 }; } We do both at the moment. - R. Niwa
No strong feelings but I have a slight preference for `{ }` initialization since it disallows narrowing conversions and works with types that do not have a declared constructor. So if we have to align in one direction, it would be my preference. Chris.
On Sep 19, 2024, at 2:55 PM, Ryosuke Niwa via webkit-dev <webkit-dev@lists.webkit.org> wrote:
Should we do:
struct Foo { int bar = 0; }
Or
struct Foo { int bar { 0 }; }
We do both at the moment.
- R. Niwa
_______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
+1 for { }. — Timothy Hatcher
On Sep 19, 2024, at 2:55 PM, Ryosuke Niwa via webkit-dev <webkit-dev@lists.webkit.org> wrote:
Should we do:
struct Foo { int bar = 0; }
Or
struct Foo { int bar { 0 }; }
We do both at the moment.
- R. Niwa
_______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
On 20 Sep 2024, at 7:55 AM, Ryosuke Niwa via webkit-dev <webkit-dev@lists.webkit.org> wrote:
Should we do:
struct Foo { int bar = 0; }
Or
struct Foo { int bar { 0 }; }
We do both at the moment.
- R. Niwa
I think `int bar = 0` reads better. I only ever see (and use) { } and I thought that was the proper coding style. I’m surprised it’s not in our guidelines. Jean-Yves
I prefer { } style. It can catch implicit narrowing bugs, which does not work with = style. Best regards, - Yusuke
On Sep 19, 2024, at 4:13 PM, Jean-Yves Avenard via webkit-dev <webkit-dev@lists.webkit.org> wrote:
On 20 Sep 2024, at 7:55 AM, Ryosuke Niwa via webkit-dev <webkit-dev@lists.webkit.org> wrote:
Should we do:
struct Foo { int bar = 0; }
Or
struct Foo { int bar { 0 }; }
We do both at the moment.
- R. Niwa
I think `int bar = 0` reads better.
I only ever see (and use) { } and I thought that was the proper coding style.
I’m surprised it’s not in our guidelines.
Jean-Yves _______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
+1 for { } because I like catching bugs. But also: If Chris is right that { } is the only way to handle types without constructors, and the goal is to pick one syntax, then { } is the only possible solution, because standardizing on = would require exceptions for types without constructors. Thanks, Geoff
On Sep 20, 2024, at 2:29 PM, Yusuke Suzuki via webkit-dev <webkit-dev@lists.webkit.org> wrote:
I prefer { } style. It can catch implicit narrowing bugs, which does not work with = style.
Best regards, - Yusuke
On Sep 19, 2024, at 4:13 PM, Jean-Yves Avenard via webkit-dev <webkit-dev@lists.webkit.org> wrote:
On 20 Sep 2024, at 7:55 AM, Ryosuke Niwa via webkit-dev <webkit-dev@lists.webkit.org> wrote:
Should we do:
struct Foo { int bar = 0; }
Or
struct Foo { int bar { 0 }; }
We do both at the moment.
- R. Niwa
I think `int bar = 0` reads better.
I only ever see (and use) { } and I thought that was the proper coding style.
I’m surprised it’s not in our guidelines.
Jean-Yves _______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
Looks like the majority opinion is to use { ~ }? - R. Niwa
On Sep 23, 2024, at 9:35 AM, Geoff Garen via webkit-dev <webkit-dev@lists.webkit.org> wrote:
+1 for { } because I like catching bugs.
But also: If Chris is right that { } is the only way to handle types without constructors, and the goal is to pick one syntax, then { } is the only possible solution, because standardizing on = would require exceptions for types without constructors.
Thanks, Geoff
On Sep 20, 2024, at 2:29 PM, Yusuke Suzuki via webkit-dev <webkit-dev@lists.webkit.org> wrote:
I prefer { } style. It can catch implicit narrowing bugs, which does not work with = style.
Best regards, - Yusuke
On Sep 19, 2024, at 4:13 PM, Jean-Yves Avenard via webkit-dev <webkit-dev@lists.webkit.org> wrote:
On 20 Sep 2024, at 7:55 AM, Ryosuke Niwa via webkit-dev <webkit-dev@lists.webkit.org> wrote:
Should we do:
struct Foo { int bar = 0; }
Or
struct Foo { int bar { 0 }; }
We do both at the moment.
- R. Niwa
I think `int bar = 0` reads better.
I only ever see (and use) { } and I thought that was the proper coding style.
I’m surprised it’s not in our guidelines.
Jean-Yves _______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
_______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
+1 for { } as well, for all the mentioned good reasons. But just to be complete, there's one exception that should probably be noted if/when we document this: For classes that have a constructor taking `std::initializer_list<T>` and other constructors that take `T` (+ conversions), using braced-initialization will *always* call the initializer_list one! In this case, we'd need to use parentheses if another constructor was wanted. E.g.: `std::vector<int> vb { 3, 2 }` -> [ 3 2 ], vs `std::vector<int> vp(3, 2);` -> [ 2 2 2 ] (3 copies of value 2)! At a glance, I haven't found WK classes that have both types of competing constructors together, so hopefully this should be a rare issue. May be worth another guideline, like "If you define a constructor taking std::initializer_list<type>, avoid defining other constructors for similar types"? Cheers, Gerald
On 24 Sep 2024, at 02:35, Geoff Garen via webkit-dev <webkit-dev@lists.webkit.org> wrote:
+1 for { } because I like catching bugs.
But also: If Chris is right that { } is the only way to handle types without constructors, and the goal is to pick one syntax, then { } is the only possible solution, because standardizing on = would require exceptions for types without constructors.
Thanks, Geoff
On Sep 20, 2024, at 2:29 PM, Yusuke Suzuki via webkit-dev <webkit-dev@lists.webkit.org> wrote:
I prefer { } style. It can catch implicit narrowing bugs, which does not work with = style.
Best regards, - Yusuke
On Sep 19, 2024, at 4:13 PM, Jean-Yves Avenard via webkit-dev <webkit-dev@lists.webkit.org> wrote:
On 20 Sep 2024, at 7:55 AM, Ryosuke Niwa via webkit-dev <webkit-dev@lists.webkit.org> wrote:
Should we do:
struct Foo { int bar = 0; }
Or
struct Foo { int bar { 0 }; }
We do both at the moment.
- R. Niwa
I think `int bar = 0` reads better.
I only ever see (and use) { } and I thought that was the proper coding style.
I’m surprised it’s not in our guidelines.
Jean-Yves _______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
_______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
Sounds like the majority of people would prefer to use { ~ }. We should probably codify that in our code style guidelines.
On Sep 23, 2024, at 5:15 PM, Gerald Squelart via webkit-dev <webkit-dev@lists.webkit.org> wrote:
+1 for { } as well, for all the mentioned good reasons.
But just to be complete, there's one exception that should probably be noted if/when we document this: For classes that have a constructor taking `std::initializer_list<T>` and other constructors that take `T` (+ conversions), using braced-initialization will *always* call the initializer_list one! In this case, we'd need to use parentheses if another constructor was wanted. E.g.: `std::vector<int> vb { 3, 2 }` -> [ 3 2 ], vs `std::vector<int> vp(3, 2);` -> [ 2 2 2 ] (3 copies of value 2)! At a glance, I haven't found WK classes that have both types of competing constructors together, so hopefully this should be a rare issue. May be worth another guideline, like "If you define a constructor taking std::initializer_list<type>, avoid defining other constructors for similar types"?
Cheers, Gerald
On 24 Sep 2024, at 02:35, Geoff Garen via webkit-dev <webkit-dev@lists.webkit.org> wrote:
+1 for { } because I like catching bugs.
But also: If Chris is right that { } is the only way to handle types without constructors, and the goal is to pick one syntax, then { } is the only possible solution, because standardizing on = would require exceptions for types without constructors.
Thanks, Geoff
On Sep 20, 2024, at 2:29 PM, Yusuke Suzuki via webkit-dev <webkit-dev@lists.webkit.org> wrote:
I prefer { } style. It can catch implicit narrowing bugs, which does not work with = style.
Best regards, - Yusuke
On Sep 19, 2024, at 4:13 PM, Jean-Yves Avenard via webkit-dev <webkit-dev@lists.webkit.org> wrote:
On 20 Sep 2024, at 7:55 AM, Ryosuke Niwa via webkit-dev <webkit-dev@lists.webkit.org> wrote:
Should we do:
struct Foo { int bar = 0; }
Or
struct Foo { int bar { 0 }; }
We do both at the moment.
- R. Niwa
I think `int bar = 0` reads better.
I only ever see (and use) { } and I thought that was the proper coding style.
I’m surprised it’s not in our guidelines.
Jean-Yves _______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
_______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
_______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-dev
participants (7)
-
Chris Dumez
-
Geoff Garen
-
Gerald Squelart
-
Jean-Yves Avenard
-
Ryosuke Niwa
-
Timothy Hatcher
-
Yusuke Suzuki