Skip to content

Commit

Permalink
Make all default and value constructors explicit (jbcoe#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcoe authored Nov 19, 2023
1 parent aa5a898 commit 96c40cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
22 changes: 12 additions & 10 deletions DRAFT.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,13 @@ class indirect {

constexpr indirect();

constexpr indirect(allocator_arg_t, const Allocator& alloc);
explicit constexpr indirect(allocator_arg_t, const Allocator& alloc);

template <class U, class... Us>
explicit constexpr indirect(U&& u, Us&&... us);

template <class U, class... Us>
constexpr indirect(allocator_arg_t, const Allocator& alloc,
explicit constexpr indirect(allocator_arg_t, const Allocator& alloc,
U&& u, Us&&... us);

constexpr indirect(const indirect& other);
Expand Down Expand Up @@ -601,7 +601,7 @@ indirect(std::allocator_arg_t, Alloc, Value) -> indirect<
#### X.Y.3 Constructors [indirect.ctor]
```c++
constexpr indirect()
explicit constexpr indirect()
```

1. _Mandates_: `is_default_constructible_v<T>` is true.
Expand All @@ -612,7 +612,7 @@ constexpr indirect()
3. _Postconditions_: `*this` is not valueless.

```c++
constexpr indirect(allocator_arg_t, const Allocator& alloc);
explicit constexpr indirect(allocator_arg_t, const Allocator& alloc);
```
4. _Mandates_: `is_default_constructible_v<T>` is `true`.
Expand All @@ -630,7 +630,7 @@ explicit constexpr indirect(U&& u, Us&&... us);

```c++
template <class U, class... Us>
constexpr indirect(allocator_arg_t, const Allocator& alloc, U&& u, Us&& ...us);
explicit constexpr indirect(allocator_arg_t, const Allocator& alloc, U&& u, Us&& ...us);
```
8. _Constraints_: `is_constructible_v<T, U, Us...>` is `true`.
Expand Down Expand Up @@ -1064,13 +1064,15 @@ class polymorphic {
using pointer = typename allocator_traits<Allocator>::pointer;
using const_pointer = typename allocator_traits<Allocator>::const_pointer;

constexpr polymorphic();
explicit constexpr polymorphic();

explicit constexpr polymorphic(allocator_arg_t, const Allocator& alloc);

template <class U, class... Ts>
explicit constexpr polymorphic(in_place_type_t<U>, Ts&&... ts);

template <class U, class... Ts>
constexpr polymorphic(allocator_arg_t, const Allocator& alloc,
explicit constexpr polymorphic(allocator_arg_t, const Allocator& alloc,
in_place_type_t<U>, Ts&&... ts);

constexpr polymorphic(const polymorphic& other);
Expand Down Expand Up @@ -1111,7 +1113,7 @@ class polymorphic {
#### X.Z.3 Constructors [polymorphic.ctor]
```c++
constexpr polymorphic()
explicit constexpr polymorphic()
```

1. _Mandates_: `is_default_constructible_v<T>` is `true`,
Expand All @@ -1123,7 +1125,7 @@ constexpr polymorphic()
3. _Postconditions_: `*this` is not valueless.

```c++
constexpr polymorphic(allocator_arg_t, const Allocator& alloc);
explicit constexpr polymorphic(allocator_arg_t, const Allocator& alloc);
```
4. _Mandates_: `is_default_constructible_v<T>` is `true`,
Expand All @@ -1143,7 +1145,7 @@ explicit constexpr polymorphic(in_place_type_t<U>, Ts&&... ts);

```c++
template <class U, class... Ts>
constexpr polymorphic(allocator_arg_t, const Allocator& alloc,
explicit constexpr polymorphic(allocator_arg_t, const Allocator& alloc,
in_place_type_t<U>, Ts&&... ts);
```
Expand Down
8 changes: 5 additions & 3 deletions indirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ class indirect {
using pointer = typename allocator_traits::pointer;
using const_pointer = typename allocator_traits::const_pointer;

constexpr indirect()
explicit constexpr indirect()
requires std::is_default_constructible_v<A>
{
static_assert(std::is_default_constructible_v<T>);
p_ = construct_from(alloc_);
}

constexpr indirect(std::allocator_arg_t, const A& alloc) : alloc_(alloc) {
explicit constexpr indirect(std::allocator_arg_t, const A& alloc)
: alloc_(alloc) {
static_assert(std::is_default_constructible_v<T>);
p_ = construct_from(alloc_);
}
Expand All @@ -89,7 +90,8 @@ class indirect {
}

template <class U, class... Us>
constexpr indirect(std::allocator_arg_t, const A& alloc, U&& u, Us&&... us)
explicit constexpr indirect(std::allocator_arg_t, const A& alloc, U&& u,
Us&&... us)
requires(std::constructible_from<T, U &&, Us && ...> &&
!std::is_same_v<std::remove_cvref_t<U>, indirect>)
: alloc_(alloc) {
Expand Down
9 changes: 5 additions & 4 deletions polymorphic.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class polymorphic {
using pointer = typename allocator_traits::pointer;
using const_pointer = typename allocator_traits::const_pointer;

constexpr polymorphic() {
explicit constexpr polymorphic() {
static_assert(std::is_default_constructible_v<T>);
using cb_allocator = typename std::allocator_traits<
A>::template rebind_alloc<detail::direct_control_block<T, T, A>>;
Expand All @@ -128,7 +128,8 @@ class polymorphic {
}
}

constexpr polymorphic(std::allocator_arg_t, const A& alloc) : alloc_(alloc) {
explicit constexpr polymorphic(std::allocator_arg_t, const A& alloc)
: alloc_(alloc) {
static_assert(std::is_default_constructible_v<T>);
using cb_allocator = typename std::allocator_traits<
A>::template rebind_alloc<detail::direct_control_block<T, T, A>>;
Expand Down Expand Up @@ -164,8 +165,8 @@ class polymorphic {
}

template <class U, class... Ts>
constexpr polymorphic(std::allocator_arg_t, const A& alloc,
std::in_place_type_t<U>, Ts&&... ts)
explicit constexpr polymorphic(std::allocator_arg_t, const A& alloc,
std::in_place_type_t<U>, Ts&&... ts)
requires std::constructible_from<U, Ts&&...> &&
std::copy_constructible<U> &&
(std::derived_from<U, T> || std::same_as<U, T>)
Expand Down

0 comments on commit 96c40cd

Please sign in to comment.