How I can keep aggregate initialization while also adding custom constructors?

You can’t have your cake and eat it too. If the object has a constructor it is no longer an aggregate, and only aggregates can be initialized with designated initializers. You can’t use constructors for arbitrary initialization logic with aggregates.

Are we toasted though? No, because there’s the “named constructor” idiom. It’s essentially just a static member function that returns an initialized object, and able to perform some logic. The idiom is compatible with aggregate initialization.

struct Foo {
    int x, y;
    static Foo filled_with(int value) {
        return {.x = value, .y = value};
    }
};

Foo foo = {.y = 1}; // Still an aggregate.
Foo foo2 = Foo::filled_with(2); // Custom logic

There’s not even any copying or moving with this approach, because C++17 removed the possibility for those. foo2 is initialized directly with whatever the static member does.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)