Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 16, 2020 Struct literals limitation | ||||
---|---|---|---|---|
| ||||
Why doesn't it compile? > auto foo() { > struct Foo { > int a, b; > } > return Foo({ > a: 10, > b: 20 > }); > } Valid C++14: > auto foo() { > struct Foo { > int a, b; > }; > return Foo({ > .a = 10, > .b = 20 > }); > } |
November 16, 2020 Re: Struct literals limitation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | On Monday, 16 November 2020 at 10:22:49 UTC, Jack Applegame wrote: > Why doesn't it compile? >> auto foo() { >> struct Foo { >> int a, b; >> } >> return Foo({ >> a: 10, >> b: 20 >> }); >> } Because in-place struct initialization has never been implemented. Once the named parameters DIP is implemented, you should be able to do that by removing the {}: https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1030.md |
November 16, 2020 Re: Struct literals limitation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | On Monday, 16 November 2020 at 10:22:49 UTC, Jack Applegame wrote:
>
> Valid C++14:
>> auto foo() {
>> struct Foo {
>> int a, b;
>> };
>> return Foo({
>> .a = 10,
>> .b = 20
>> });
>> }
Nope:
/tmp % clang++ -c -std=c++14 -pedantic cpp.cpp 1 :(
cpp.cpp:6:13: warning: designated initializers are a C++20 extension [-Wc++20-designator]
.a = 10,
^
1 warning generated.
|
November 17, 2020 Re: Struct literals limitation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | On Monday, 16 November 2020 at 10:22:49 UTC, Jack Applegame wrote: > Why doesn't it compile? >> auto foo() { >> struct Foo { >> int a, b; >> } >> return Foo({ >> a: 10, >> b: 20 >> }); >> } > > Valid C++14: >> auto foo() { >> struct Foo { >> int a, b; >> }; >> return Foo({ >> .a = 10, >> .b = 20 >> }); >> } Foo foo = { a: 10, b: 20 } is working in D at the moment. I can't wait for better initialization, named arguments to come into play too. It's a shame that in some cases C is more expressive than D: https://github.com/gfx-rs/wgpu-native/blob/master/examples/triangle/main.c , it even allows to return a pointer to a struct literal. |
Copyright © 1999-2021 by the D Language Foundation