C++ Logo

std-proposals

Advanced search

[std-proposals] Generalized "default" constructor

From: Gergely Nagy <gergely.nagy.alt_at_[hidden]>
Date: Tue, 14 Feb 2023 23:12:28 +0100
Hey,

Been wondering. To use emplace_back or emplace, or try_emplace, one needs a
constructor obviously that can be called. Now if I have a trivial struct,
like

struct Trivial {
    int x;
    double y;
    std::string z;
};

I have to write my own constructor to be able to use *emplace*, even though
it is kinda trivial, what I mean when I write:

std::vector<Trivial> t;
t.emplace_back(1, 2.0, "blah");

So what if, to make this possible, the following feature would be added:

struct Trivial {
    *Trivial(auto&&...) = default;*

    int x;
    double y;
    std::string z;
};

which would be equivalent to

struct Trivial {
* Trivial(auto&& ax, auto&& ay, auto&& az)*
* : x(std::forward<decltype(ax)>(ax))*

* , y(std::forward<decltype(ay)>(ay)) ,
z(std::forward<decltype(az)>(az)) {}*

    int x;
    double y;
    std::string z;
};

"similarly" to a default constructor.

I admit I haven't thought through all the potential problems with such a
construct.

Thanks,
   Gergely

Received on 2023-02-14 22:12:40