Skip to content

Commit

Permalink
Remove std::in_place argument to indirect constructor (#128)
Browse files Browse the repository at this point in the history
The constructor argument is just noise. Consistency with polymorphic is artificial.

@BengtGustafsson pointed this out some time ago and I've not done anything about it.
  • Loading branch information
jbcoe committed Nov 8, 2023
1 parent dec9df9 commit f2315a3
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 172 deletions.
8 changes: 4 additions & 4 deletions DRAFT.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,11 @@ class indirect {
constexpr indirect();
template <class... Ts>
explicit constexpr indirect(std::in_place_t, Ts&&... ts);
explicit constexpr indirect(Ts&&... ts);
template <class... Ts>
constexpr indirect(
std::allocator_arg_t, const Allocator& alloc, std::in_place_t, Ts&&... ts);
std::allocator_arg_t, const Allocator& alloc, Ts&&... ts);
constexpr indirect(const indirect& other);
Expand Down Expand Up @@ -374,7 +374,7 @@ constexpr indirect()

```c++
template <class... Ts>
explicit constexpr indirect(std::in_place_t, Ts&&... ts);
explicit constexpr indirect(Ts&&... ts);
```
* _Constraints_: `is_constructible_v<T, Ts...>` is true.
Expand All @@ -387,7 +387,7 @@ explicit constexpr indirect(std::in_place_t, Ts&&... ts);
```c++
template <class... Ts>
constexpr indirect(
std::allocator_arg_t, const Allocator& alloc, std::in_place_t, Ts&&... ts);
std::allocator_arg_t, const Allocator& alloc, Ts&&... ts);
```

* _Constraints_: `is_constructible_v<T, Ts...>` is true.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class Composite {
xyz::indirect<B> b_; // b_ owns an object of type B
public:
Composite(const A& a, const B& b) :
a_(std::in_place, a),
b_(std::in_place, b) {}
a_(a),
b_(b) {}

// ...
};
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/indirect_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static void Indirect_BM_VectorAccumulate_UniquePointer(
}

static void Indirect_BM_Copy_Indirect(benchmark::State& state) {
auto p = xyz::indirect<A>(std::in_place, 42);
auto p = xyz::indirect<A>(42);
for (auto _ : state) {
auto pp = p;
benchmark::DoNotOptimize(pp);
Expand All @@ -181,7 +181,7 @@ static void Indirect_BM_VectorCopy_Indirect(benchmark::State& state) {
std::vector<xyz::indirect<A>> v;
v.reserve(LARGE_VECTOR_SIZE);
for (size_t i = 0; i < LARGE_VECTOR_SIZE; ++i) {
v.push_back(xyz::indirect<A>(std::in_place, i));
v.push_back(xyz::indirect<A>(i));
}

for (auto _ : state) {
Expand All @@ -193,7 +193,7 @@ static void Indirect_BM_VectorCopy_Indirect(benchmark::State& state) {
static void Indirect_BM_ArrayCopy_Indirect(benchmark::State& state) {
std::array<std::optional<xyz::indirect<A>>, LARGE_ARRAY_SIZE> v;
for (size_t i = 0; i < v.size(); ++i) {
v[i] = std::optional<xyz::indirect<A>>(xyz::indirect<A>(std::in_place, i));
v[i] = std::optional<xyz::indirect<A>>(xyz::indirect<A>(i));
}

for (auto _ : state) {
Expand All @@ -206,7 +206,7 @@ static void Indirect_BM_VectorAccumulate_Indirect(benchmark::State& state) {
std::vector<xyz::indirect<A>> v;
v.reserve(LARGE_VECTOR_SIZE);
for (size_t i = 0; i < LARGE_VECTOR_SIZE; ++i) {
v.push_back(xyz::indirect<A>(std::in_place, i));
v.push_back(xyz::indirect<A>(i));
}

for (auto _ : state) {
Expand Down
48 changes: 24 additions & 24 deletions compile_checks/indirect_consteval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ struct ConstexprHashable {};
} // namespace xyz::testing
template <>
struct std::hash<xyz::testing::ConstexprHashable> {
constexpr std::size_t operator()(const xyz::testing::ConstexprHashable& key) const {
constexpr std::size_t operator()(
const xyz::testing::ConstexprHashable& key) const {
return 0;
}
};
Expand All @@ -42,16 +43,15 @@ consteval bool indirect_default_construction() {
static_assert(indirect_default_construction(),
"constexpr function call failed");

consteval bool indirect_in_place_construction() {
auto i = xyz::indirect<int>(std::in_place, 42);
consteval bool indirect_construction() {
auto i = xyz::indirect<int>(42);
return true;
}
static_assert(indirect_in_place_construction(),
static_assert(indirect_construction(),
"constexpr function call failed");

consteval bool indirect_allocator_construction() {
auto i = xyz::indirect<int>(std::allocator_arg, std::allocator<int>{},
std::in_place, 42);
auto i = xyz::indirect<int>(std::allocator_arg, std::allocator<int>{}, 42);
return true;
}
static_assert(indirect_allocator_construction(),
Expand Down Expand Up @@ -105,94 +105,94 @@ consteval bool indirect_move_assignment() {
static_assert(indirect_move_assignment(), "constexpr function call failed");

consteval bool indirect_object_access() {
auto i = xyz::indirect<int>(std::in_place, 42);
auto i = xyz::indirect<int>(42);
return *i == 42;
}
static_assert(indirect_object_access(), "constexpr function call failed");

consteval bool indirect_const_object_access() {
const auto i = xyz::indirect<int>(std::in_place, 42);
const auto i = xyz::indirect<int>(42);
return *i == 42;
}
static_assert(indirect_const_object_access(), "constexpr function call failed");

consteval bool indirect_operator_arrow() {
auto i = xyz::indirect<int>(std::in_place, 42);
auto i = xyz::indirect<int>(42);
return *(i.operator->()) == 42;
}
static_assert(indirect_operator_arrow(), "constexpr function call failed");

consteval bool indirect_const_operator_arrow() {
const auto i = xyz::indirect<int>(std::in_place, 42);
const auto i = xyz::indirect<int>(42);
return *(i.operator->()) == 42;
}
static_assert(indirect_const_operator_arrow(),
"constexpr function call failed");

consteval bool indirect_swap() {
auto i = xyz::indirect<int>(std::in_place, 42);
auto ii = xyz::indirect<int>(std::in_place, 101);
auto i = xyz::indirect<int>(42);
auto ii = xyz::indirect<int>(101);
using std::swap;
swap(i, ii);
return *i == 101 && *ii == 42;
}
static_assert(indirect_swap(), "constexpr function call failed");

consteval bool indirect_member_swap() {
auto i = xyz::indirect<int>(std::in_place, 42);
auto ii = xyz::indirect<int>(std::in_place, 101);
auto i = xyz::indirect<int>(42);
auto ii = xyz::indirect<int>(101);
i.swap(ii);
return *i == 101 && *ii == 42;
}
static_assert(indirect_member_swap(), "constexpr function call failed");

consteval bool indirect_valueless_after_move() {
auto i = xyz::indirect<int>(std::in_place, 42);
auto i = xyz::indirect<int>(42);
auto ii = std::move(i);
return i.valueless_after_move() && !ii.valueless_after_move();
}
static_assert(indirect_valueless_after_move(),
"constexpr function call failed");

consteval bool indirect_equality() {
auto i = xyz::indirect<int>(std::in_place, 42);
auto ii = xyz::indirect<int>(std::in_place, 42);
auto i = xyz::indirect<int>(42);
auto ii = xyz::indirect<int>(42);
return i == ii;
}
static_assert(indirect_equality(), "constexpr function call failed");

consteval bool indirect_inequality() {
auto i = xyz::indirect<int>(std::in_place, 42);
auto ii = xyz::indirect<int>(std::in_place, 101);
auto i = xyz::indirect<int>(42);
auto ii = xyz::indirect<int>(101);
return i != ii;
}
static_assert(indirect_inequality(), "constexpr function call failed");

consteval bool indirect_three_way_comparison() {
auto i = xyz::indirect<int>(std::in_place, 42);
auto ii = xyz::indirect<int>(std::in_place, 101);
auto i = xyz::indirect<int>(42);
auto ii = xyz::indirect<int>(101);
return (i <=> ii) == (*i <=> *ii);
}
static_assert(indirect_three_way_comparison(),
"constexpr function call failed");

consteval bool indirect_and_value_equality() {
auto i = xyz::indirect<int>(std::in_place, 42);
auto i = xyz::indirect<int>(42);
int ii = 42;
return (i == ii) && (ii == i);
}
static_assert(indirect_and_value_equality(), "constexpr function call failed");

consteval bool indirect_and_value_inequality() {
auto i = xyz::indirect<int>(std::in_place, 42);
auto i = xyz::indirect<int>(42);
int ii = 101;
return (i != ii) && (ii != i);
}
static_assert(indirect_and_value_inequality(),
"constexpr function call failed");

consteval bool indirect_and_value_three_way_comparison() {
auto i = xyz::indirect<int>(std::in_place, 42);
auto i = xyz::indirect<int>(42);
int ii = 101;
return (i <=> ii) != (ii <=> i);
}
Expand Down
5 changes: 2 additions & 3 deletions indirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class indirect {
}

template <class... Ts>
explicit constexpr indirect(std::in_place_t, Ts&&... ts)
explicit constexpr indirect(Ts&&... ts)
requires std::constructible_from<T, Ts&&...>
{
auto mem = allocator_traits::allocate(alloc_, 1);
Expand All @@ -80,8 +80,7 @@ class indirect {
}

template <class... Ts>
constexpr indirect(std::allocator_arg_t, const A& alloc, std::in_place_t,
Ts&&... ts)
constexpr indirect(std::allocator_arg_t, const A& alloc, Ts&&... ts)
requires std::constructible_from<T, Ts&&...>
: alloc_(alloc) {
auto mem = allocator_traits::allocate(alloc_, 1);
Expand Down
Loading

0 comments on commit f2315a3

Please sign in to comment.