C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Fixing Inheritance of Constructors from Aggregate bases

From: David Ledger <DavidLedger_at_[hidden]>
Date: Sun, 7 Apr 2024 06:24:10 +0000
Hello!

Neither option A nor B would allow for designated initializers being using for non-inherited objects.

Option B proposes behaviour consistent with inheritance of constructors (for example):

    struct Foo
    {
        Foo(int, int);
    };

    struct Bar : Foo
    {
        using Foo::Foo;
        int a = 0;
    };

It would not be possible to do this:

    auto bar = Bar{1, 2, 3};

Or this:

    auto bar = Bar{Foo{1, 2}, 3};

Once the `using Foo::Foo` is applied, all data members and base classes are default initialized (except the base which the constructor was inherited from).

Kind regards,
    David Ledger

From: Zhihao Yuan <zy_at_miator.net>
Sent: Sunday, April 7, 2024 4:00 PM
To: std-proposals_at_[hidden]
Cc: David Ledger <DavidLedger_at_[hidden]>
Subject: Re: [std-proposals] Fixing Inheritance of Constructors from Aggregate bases

If I have

    struct Foo { int a, b; }
    struct Bar : Foo { using Foo::Foo; int c; }

Can I do

    auto bar = Bar{.a = 1, .b = 2, .c = 3}; // ?

if so, I think it might remove some concerns
over Designated-initializers for Base Classes (open-std.org)<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2287r2.html>

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
_______________________________________________

On Saturday, April 6th, 2024 at 9:36 PM, David Ledger via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>> wrote:


Hello everybody!!
Right now, this will not compile:


struct Foo { int a, b; }

struct Bar : Foo { using Foo::Foo; }

auto bar = Bar{1, 2}; // ERROR


Which is confusing and unintuitive. This paper proposes an improvement to this behaviour.
https://seppeon.gitlab.io/cpp-proposals/inheritance-of-aggregate-initialization.html#_abstract

Regards,
David Ledger

Received on 2024-04-07 06:24:17