c++: avoid initializer_list<string> [PR105838]
commitd081807d8d70e3e87eae41e1560e54d503f4d465
authorJason Merrill <jason@redhat.com>
Tue, 6 Dec 2022 14:51:51 +0000 (6 09:51 -0500)
committerJason Merrill <jason@redhat.com>
Thu, 8 Dec 2022 18:40:21 +0000 (8 13:40 -0500)
tree185855bcdb475ae4a4034cbb4441d20d661fa927
parent1e1847612d7f169f82c985b0b3a5e3301d6fe999
c++: avoid initializer_list<string> [PR105838]

When constructing a vector<string> from { "strings" }, first is built an
initializer_list<string>, which is then copied into the strings in the
vector.  But this is inefficient: better would be treat the { "strings" }
as a range and construct the strings in the vector directly from the
string-literals.  We can do this transformation for standard library
classes because we know the design patterns they follow.

PR c++/105838

gcc/cp/ChangeLog:

* call.cc (list_ctor_element_type): New.
(braced_init_element_type): New.
(has_non_trivial_temporaries): New.
(maybe_init_list_as_array): New.
(maybe_init_list_as_range): New.
(build_user_type_conversion_1): Use maybe_init_list_as_range.
* parser.cc (cp_parser_braced_list): Call
recompute_constructor_flags.
* cp-tree.h (find_temps_r): Declare.

gcc/testsuite/ChangeLog:

* g++.dg/tree-ssa/initlist-opt1.C: New test.
gcc/cp/call.cc
gcc/cp/cp-tree.h
gcc/cp/parser.cc
gcc/testsuite/g++.dg/tree-ssa/initlist-opt1.C [new file with mode: 0644]