C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Generalized "default" constructor

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Wed, 15 Feb 2023 00:19:17 +0200
On Wed, 15 Feb 2023 at 00:12, Gergely Nagy via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> 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");

That code just works from C++20 onwards, since C++20 allows
initializing aggregates with the parenthesized syntax,
courtesy of Thomas Köppe and yours truly.

Received on 2023-02-14 22:19:30