C++ Logo

std-proposals

Advanced search

[std-proposals] Designated Initializers: allow non-direct members

From: Jonas Christen <admin_at_[hidden]>
Date: Tue, 25 Feb 2025 22:18:10 +0100
Designated initializers are only allowed for direct non-static data
members right now as the standard says:

> [...] the identifier in each designator shall name a direct
    non-static data member of the class [...]

and I couldn't find any explicit reasoning for this decision in P0329R4:
"Designated Initialization Wording".

I'm not a language/compiler expert, so not sure if there would be any
certain problems with dropping the "direct" here (or maybe rewording it
differently ofc) and implementing it, so please let me know if there are.

Basically I want something like this to compile:

    struct ta{ int a; };
    struct tb{ int b; };
    struct tc : ta, tb{
         using ta::a;
         using tb::b;
         int c;
    };
    struct td{ int a; int b; int c; };
    int main(){
         // OK
         td ok{ .a = 1, .b = 2, .c = 3 };

         // Error: 'ta::a' is not a direct member of 'tc'
         // would be allowed with relaxed rules for designated-initializers
         ta err{ .a = 1, .b = 2, .c = 3 };
    }


- Jonas

Received on 2025-02-25 21:18:12