This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] PR77528 add default constructors for container adaptors


The standard says that the container adaptors have a constructor with
a default argument, which serves as a default constructor. That
involves default-constructing the underlying sequence as the default
argument and then move-constructing the member variable from that
argument. Because std::deque allocates memory in its move constructor
this means the default constructor of an adaptor using std::deque
will allocate twice, which is wasteful and expensive.

This change adds a separate default constructor, defined as defaulted
(and adding default member-initializers to ensure the member variables
get value-initialized). This avoids the move-construction, so we only
allocate once when using std::deque.

As noted in Bugzilla this makes the default constructors non-explicit,
which is a change in behaviour, and not strictly conforming. I'm doing
that intentionally, because having default construction be explicit
for these types is stupid anyway.

Because the default constructor is defined as defaulted it will be
deleted when the underlying sequence isn't default constructible, so
we can still explicitly instantiate the adaptors in such cases (which
I've added tests for).

I'm also shuffling some tests around. Currently we have two explicit
instantiation tests for each adaptor, 1.cc and 1_c++0x.cc, which
originally tested it for C++98 and C++11. But since the default became
gnu++14 we now test twice in C++14 mode. So I'm replacing 1_c++0x.cc
with 1_c++98.cc which has { dg-options "-std=gnu++98.cc" } to force
testing that dialect.

	PR libstdc++/77528
	* include/bits/stl_queue.h (queue::c): Add default member initializer.
	(queue::queue()): Add constructor and define as defaulted.
	(queue::queue(_Sequence&&)): Remove default argument.
	(priority_queue::c, priority_queue::comp): Add default member
	initializers.
	(priority_queue::priority_queue()): Add constructor and define as
	defaulted.
	(priority_queue::priority_queue(const _Compare&, _Sequence&&)):
	Remove default argument for first parameter.
	* include/bits/stl_stack.h (stack::c): Add default member initializer.
	(stack::stack()): Add constructor and define as defaulted.
	(stack::stack(const _Sequence&)): Remove default argument.
	* testsuite/23_containers/priority_queue/requirements/
	explicit_instantiation/1.cc: Test explicit instantiation with
	non-DefaultConstructible sequence.
	* testsuite/23_containers/priority_queue/77528.cc: New test.
	* testsuite/23_containers/priority_queue/requirements/
	explicit_instantiation/1_c++0x.cc: Replace with 1_c++98.cc.
	* testsuite/23_containers/queue/77528.cc: New test.
	* testsuite/23_containers/queue/requirements/explicit_instantiation/
	1.cc: Test explicit instantiation with non-DefaultConstructible
	sequence.
	* testsuite/23_containers/queue/requirements/explicit_instantiation/
	1_c++0x.cc: Replace with 1_c++98.cc.
	* testsuite/23_containers/stack/77528.cc: New test.
	* testsuite/23_containers/stack/requirements/explicit_instantiation/
	1.cc: Test explicit instantiation with non-DefaultConstructible
	sequence.
	* testsuite/23_containers/stack/requirements/explicit_instantiation/
	1_c++0x.cc: Replace with 1_c++98.cc.

Tested powerpc64le-linux, committed to trunk.

We might want to backport this to the branches, but I'll let it bake
on trunk for a bit first.

Attachment: patch.txt
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]