Several SO posts are telling that implicit-lifetime object creation has been introduced with C++20 and, indeed, I can’t find mention of it in drafts for older C++.
Yet cppreference does not indicate a restriction for the langage version, except this cryptic (to me) DR at the bottom of https://en.cppreference.com/w/cpp/named_req/ImplicitLifetimeType:
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 2489 | C++20 | an operation that begins the lifetime of a char array implicitly creates objects | it does not |
So does implicit lifetime object creation apply before C++20?
2
The DR is from the C++23 version of the standard, so it is being applied back to C++20 where this feature started.
So no, implicit lifetime types is a C++20 and above feature.
Once a standard is published, ISO doesn’t go back and change the past. If you read the C++17 standard, you won’t find any mention of implicit object creation.
However, standard modes older than C++20 in compilers (even compilers that are older than C++20 itself) still “support” implicit object creation in the sense that, if you try to do something that triggers implicit object creation in C++20 to make it well-defined, and you compile it in C++17 mode or earlier, the behavior will match what would have happened with implicit object creation. That’s because, prior to C++20, an operation that could only be well-defined with implicit object creation would instead have undefined behaviour, but the manifestation of that undefined behaviour in all real compilers was simply to act as if the object were there, and not “exploit” the fact that an object of the required type was never created. Indeed, this was necessary so that standard libraries could implement containers like std::vector
without using compiler magic.
In that sense, while implicit object creation is a C++20 feature, it has been supported by all C++ compilers forever. Though, of course, std::start_lifetime_as
(which was a piece of the implicit object creation proposal that was split off into a separate paper and approved for C++23) will not exist on compilers that don’t support C++20.
2