diff options
author | Marek Polacek <polacek@redhat.com> | 2020-07-10 17:39:54 -0400 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2020-08-12 20:53:35 -0400 |
commit | 2ec803e768a820c9208fea2e8b144ee359379139 (patch) | |
tree | c4b048520c0be28004ec167a05e26136311aa62d /gcc/cp | |
parent | afdd220a0ccf9d5a689b6aceccd8327213a51b9b (diff) | |
download | gcc-2ec803e768a820c9208fea2e8b144ee359379139.zip gcc-2ec803e768a820c9208fea2e8b144ee359379139.tar.gz gcc-2ec803e768a820c9208fea2e8b144ee359379139.tar.bz2 |
c++: Fixing the wording of () aggregate-init [PR92812]
P1975R0 tweaks the static_cast wording: it says that "An expression e can be
explicitly converted to a type T if [...] T is an aggregate type having a first
element x and there is an implicit conversion sequence from e to the type of
x." This already works for classes, e.g.:
struct Aggr { int x; int y; };
Aggr a = static_cast<Aggr>(1);
for which we create TARGET_EXPR <D.2111, {.x=1}>.
The proposal also mentions "If T is ``array of unknown bound of U'',
this direct-initialization defines the type of the expression as U[1]" which
suggest that this should work for arrays (they're aggregates too, after all):
int (&&r)[3] = static_cast<int[3]>(42);
int (&&r2)[1] = static_cast<int[]>(42);
So I handled that specifically in build_static_cast_1: wrap the
expression in { } and initialize from that. For the 'r' case above
this creates TARGET_EXPR <D.2083, {42}>.
There are multiple things in play, as usual, so the tests test brace
elision, narrowing, explicit constructors, and lifetime extension too.
I think it's in line with what we discussed on the core reflector.
gcc/cp/ChangeLog:
PR c++/92812
* typeck.c (build_static_cast_1): Implement P1975R0 by allowing
static_cast to aggregate type.
gcc/testsuite/ChangeLog:
PR c++/92812
* g++.dg/cpp2a/paren-init27.C: New test.
* g++.dg/cpp2a/paren-init28.C: New test.
* g++.dg/cpp2a/paren-init29.C: New test.
* g++.dg/cpp2a/paren-init30.C: New test.
* g++.dg/cpp2a/paren-init31.C: New test.
* g++.dg/cpp2a/paren-init32.C: New test.
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/typeck.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index a557f34..9166156 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -7480,6 +7480,20 @@ build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p, t. */ result = perform_direct_initialization_if_possible (type, expr, c_cast_p, complain); + /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42), + which initialize the first element of the aggregate. We need to handle + the array case specifically. */ + if (result == NULL_TREE + && cxx_dialect >= cxx20 + && TREE_CODE (type) == ARRAY_TYPE) + { + /* Create { EXPR } and perform direct-initialization from it. */ + tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr); + CONSTRUCTOR_IS_DIRECT_INIT (e) = true; + CONSTRUCTOR_IS_PAREN_INIT (e) = true; + result = perform_direct_initialization_if_possible (type, e, c_cast_p, + complain); + } if (result) { if (processing_template_decl) |