Skip to content

Commit ebe5483

Browse files
committed
Just testing
1 parent af5477f commit ebe5483

File tree

8 files changed

+91
-40
lines changed

8 files changed

+91
-40
lines changed

BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cc_library(
1313
srcs = ["indirect.cc"],
1414
hdrs = ["indirect.h"],
1515
copts = ["-Iexternal/value_types/"],
16-
defines = ["XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION"],
16+
defines = [],
1717
visibility = ["//visibility:public"],
1818
deps = ["feature_check"],
1919
)
@@ -92,7 +92,7 @@ cc_library(
9292
srcs = ["polymorphic.cc"],
9393
hdrs = ["polymorphic.h"],
9494
copts = ["-Iexternal/value_types/"],
95-
defines = ["XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION"],
95+
defines = [],
9696
visibility = ["//visibility:public"],
9797
)
9898

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ target_sources(xyz_value_types
3939
xyz_add_library(
4040
NAME indirect
4141
ALIAS xyz_value_types::indirect
42-
DEFINITIONS XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
4342
)
4443
target_sources(indirect
4544
INTERFACE
@@ -76,7 +75,6 @@ target_sources(indirect_cxx17
7675
xyz_add_library(
7776
NAME polymorphic
7877
ALIAS xyz_value_types::polymorphic
79-
DEFINITIONS XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
8078
)
8179
target_sources(polymorphic
8280
INTERFACE

indirect.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3131

3232
namespace xyz {
3333

34+
#ifndef XYZ_TYPE_IDENTITY_DEFINED
35+
#define XYZ_TYPE_IDENTITY_DEFINED
36+
#ifdef XYZ_HAS_STD_TYPE_IDENTITY
37+
using std::type_identity_t;
38+
#else
39+
template <class T>
40+
struct type_identity { using type = T; };
41+
template <class T>
42+
using type_identity_t = typename type_identity<T>::type;
43+
#endif // XYZ_HAS_STD_TYPE_IDENTITY
44+
#endif // XYZ_TYPE_IDENTITY_DEFINED
45+
3446
#ifndef XYZ_UNREACHABLE_DEFINED
3547
#define XYZ_UNREACHABLE_DEFINED
3648

@@ -182,7 +194,7 @@ class indirect {
182194
p_ = construct_from(alloc_, ilist, std::forward<Us>(us)...);
183195
}
184196

185-
constexpr indirect(std::allocator_arg_t, const std::type_identity_t<A>& alloc,
197+
constexpr indirect(std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
186198
const indirect& other)
187199
: alloc_(alloc) {
188200
static_assert(std::copy_constructible<T>);
@@ -195,7 +207,7 @@ class indirect {
195207
}
196208

197209
constexpr indirect(
198-
std::allocator_arg_t, const std::type_identity_t<A>& alloc,
210+
std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
199211
indirect&& other) noexcept(allocator_traits::is_always_equal::value)
200212
: p_(nullptr), alloc_(alloc) {
201213
static_assert(std::move_constructible<T>);
@@ -465,16 +477,12 @@ concept is_hashable = requires(T t) { std::hash<T>{}(t); };
465477
template <typename Value>
466478
indirect(Value) -> indirect<Value>;
467479

468-
template <typename Alloc, typename Value,
469-
typename std::enable_if_t<!is_indirect_v<Value>, int> = 0>
470-
indirect(std::allocator_arg_t, Alloc, Value) -> indirect<
471-
Value, typename std::allocator_traits<Alloc>::template rebind_alloc<Value>>;
472-
473-
#ifdef XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
474480
template <typename Alloc, typename Value>
475-
indirect(std::allocator_arg_t, std::type_identity_t<Alloc>,
481+
indirect(std::allocator_arg_t, Alloc, Value) -> indirect<Value, Alloc>;
482+
483+
template <typename Alloc, typename Alloc2, typename Value>
484+
indirect(std::allocator_arg_t, Alloc2,
476485
indirect<Value, Alloc>) -> indirect<Value, Alloc>;
477-
#endif // XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
478486

479487
} // namespace xyz
480488

indirect_cxx14.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ namespace xyz {
3636
struct in_place_t {};
3737
#endif // XYZ_IN_PLACE_DEFINED
3838

39+
#ifndef XYZ_TYPE_IDENTITY_DEFINED
40+
#define XYZ_TYPE_IDENTITY_DEFINED
41+
#ifdef XYZ_HAS_STD_TYPE_IDENTITY
42+
using std::type_identity_t;
43+
#else
44+
template <class T>
45+
struct type_identity { using type = T; };
46+
template <class T>
47+
using type_identity_t = typename type_identity<T>::type;
48+
#endif // XYZ_HAS_STD_TYPE_IDENTITY
49+
#endif // XYZ_TYPE_IDENTITY_DEFINED
50+
3951
#ifndef XYZ_UNREACHABLE_DEFINED
4052
#define XYZ_UNREACHABLE_DEFINED
4153

@@ -575,8 +587,11 @@ template <typename Value>
575587
indirect(Value) -> indirect<Value>;
576588

577589
template <typename Alloc, typename Value>
578-
indirect(std::allocator_arg_t, Alloc, Value) -> indirect<
579-
Value, typename std::allocator_traits<Alloc>::template rebind_alloc<Value>>;
590+
indirect(std::allocator_arg_t, Alloc, Value) -> indirect<Value, Alloc>;
591+
592+
template <typename Alloc, typename Alloc2, typename Value>
593+
indirect(std::allocator_arg_t, Alloc2,
594+
indirect<Value, Alloc>) -> indirect<Value, Alloc>;
580595
#endif // XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION
581596

582597
} // namespace xyz

indirect_test.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,13 @@ TEST(IndirectTest, TemplateArgumentDeductionCopy) {
133133
}
134134

135135
TEST(IndirectTest, TemplateArgumentDeductionWithAllocator) {
136-
xyz::indirect i(std::allocator_arg, xyz::TaggedAllocator<void>(1), 42);
136+
xyz::indirect i(std::allocator_arg, xyz::TaggedAllocator<int>(1), 42);
137137

138138
static_assert(
139139
std::is_same_v<decltype(i.get_allocator()), xyz::TaggedAllocator<int>>);
140140
EXPECT_EQ(*i, 42);
141141
}
142142

143-
#ifdef XYZ_HAS_STD_TYPE_IDENTITY
144-
#ifdef XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
145143
TEST(IndirectTest, TemplateArgumentDeductionWithDeducedAllocatorAndCopy) {
146144
xyz::indirect i(std::allocator_arg, xyz::TaggedAllocator<int>(1), 42);
147145
xyz::indirect ii(std::allocator_arg, 2, i);
@@ -158,8 +156,6 @@ TEST(IndirectTest, TemplateArgumentDeductionWithDeducedAllocatorAndMove) {
158156
EXPECT_EQ(*ii, 42);
159157
EXPECT_EQ(ii.get_allocator().tag, 2);
160158
}
161-
#endif // XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
162-
#endif // XYZ_HAS_STD_TYPE_IDENTITY
163159
#endif // XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION
164160

165161
template <typename Allocator = std::allocator<void>>

polymorphic.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3333

3434
namespace xyz {
3535

36+
#ifndef XYZ_TYPE_IDENTITY_DEFINED
37+
#define XYZ_TYPE_IDENTITY_DEFINED
38+
#ifdef XYZ_HAS_STD_TYPE_IDENTITY
39+
using std::type_identity_t;
40+
#else
41+
template <class T>
42+
struct type_identity { using type = T; };
43+
template <class T>
44+
using type_identity_t = typename type_identity<T>::type;
45+
#endif // XYZ_HAS_STD_TYPE_IDENTITY
46+
#endif // XYZ_TYPE_IDENTITY_DEFINED
47+
3648
#ifndef XYZ_UNREACHABLE_DEFINED
3749
#define XYZ_UNREACHABLE_DEFINED
3850

@@ -251,7 +263,7 @@ class polymorphic {
251263
}
252264

253265
constexpr polymorphic(std::allocator_arg_t,
254-
const std::type_identity_t<A>& alloc,
266+
const xyz::type_identity_t<A>& alloc,
255267
const polymorphic& other)
256268
: alloc_(alloc) {
257269
if (!other.valueless_after_move()) {
@@ -262,7 +274,7 @@ class polymorphic {
262274
}
263275

264276
constexpr polymorphic(
265-
std::allocator_arg_t, const std::type_identity_t<A>& alloc,
277+
std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
266278
polymorphic&& other) noexcept(allocator_traits::is_always_equal::value)
267279
: alloc_(alloc) {
268280
if constexpr (allocator_traits::is_always_equal::value) {
@@ -414,19 +426,17 @@ class polymorphic {
414426
}
415427
}
416428
};
417-
#ifdef XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
429+
418430
template <typename Value>
419431
polymorphic(Value) -> polymorphic<Value>;
420432

421-
template <typename Alloc, typename Value,
422-
typename std::enable_if_t<!is_polymorphic_v<Value>, int> = 0>
423-
polymorphic(std::allocator_arg_t, Alloc, Value) -> polymorphic<
424-
Value, typename std::allocator_traits<Alloc>::template rebind_alloc<Value>>;
425-
426433
template <typename Alloc, typename Value>
427-
polymorphic(std::allocator_arg_t, std::type_identity_t<Alloc>,
434+
polymorphic(std::allocator_arg_t, Alloc, Value) -> polymorphic<Value, Alloc>;
435+
436+
template <typename Alloc, typename Alloc2, typename Value>
437+
polymorphic(std::allocator_arg_t, Alloc2,
428438
polymorphic<Value, Alloc>) -> polymorphic<Value, Alloc>;
429-
#endif // XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
439+
430440
} // namespace xyz
431441

432442
#endif // XYZ_POLYMORPHIC_H_

polymorphic_cxx14.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2727
#include <type_traits>
2828
#include <utility>
2929

30+
#include "feature_check.h"
31+
3032
#ifndef XYZ_POLYMORPHIC_HAS_EXTENDED_CONSTRUCTORS
3133
#define XYZ_POLYMORPHIC_HAS_EXTENDED_CONSTRUCTORS 1
3234
#endif // XYZ_POLYMORPHIC_HAS_EXTENDED_CONSTRUCTORS
@@ -40,6 +42,20 @@ struct in_place_type_t {};
4042
} // namespace xyz
4143
#endif // XYZ_IN_PLACE_TYPE_DEFINED
4244

45+
#ifndef XYZ_TYPE_IDENTITY_DEFINED
46+
#define XYZ_TYPE_IDENTITY_DEFINED
47+
namespace xyz {
48+
#ifdef XYZ_HAS_STD_TYPE_IDENTITY
49+
using std::type_identity_t;
50+
#else
51+
template <class T>
52+
struct type_identity { using type = T; };
53+
template <class T>
54+
using type_identity_t = typename type_identity<T>::type;
55+
#endif // XYZ_HAS_STD_TYPE_IDENTITY
56+
} // namespace xyz
57+
#endif // XYZ_TYPE_IDENTITY_DEFINED
58+
4359
#ifndef XYZ_UNREACHABLE_DEFINED
4460
#define XYZ_UNREACHABLE_DEFINED
4561

@@ -319,7 +335,7 @@ class polymorphic : private detail::empty_base_optimization<A> {
319335
typename std::remove_reference<U>::type>::type>{},
320336
std::forward<U>(u)) {}
321337

322-
polymorphic(std::allocator_arg_t, const A& alloc, const polymorphic& other)
338+
polymorphic(std::allocator_arg_t, const xyz::type_identity_t<A>& alloc, const polymorphic& other)
323339
: alloc_base(alloc) {
324340
if (!other.valueless_after_move()) {
325341
cb_ = other.cb_->clone(alloc_base::get());
@@ -335,7 +351,7 @@ class polymorphic : private detail::empty_base_optimization<A> {
335351
other) {}
336352

337353
polymorphic(
338-
std::allocator_arg_t, const A& alloc,
354+
std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
339355
polymorphic&& other) noexcept(allocator_traits::is_always_equal::value)
340356
: alloc_base(alloc) {
341357
if (allocator_traits::propagate_on_container_copy_assignment::value) {
@@ -476,6 +492,18 @@ class polymorphic : private detail::empty_base_optimization<A> {
476492
}
477493
};
478494

495+
#ifdef XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION
496+
template <typename Value>
497+
polymorphic(Value) -> polymorphic<Value>;
498+
499+
template <typename Alloc, typename Value>
500+
polymorphic(std::allocator_arg_t, Alloc, Value) -> polymorphic<Value, Alloc>;
501+
502+
template <typename Alloc, typename Alloc2, typename Value>
503+
polymorphic(std::allocator_arg_t, Alloc2,
504+
polymorphic<Value, Alloc>) -> polymorphic<Value, Alloc>;
505+
#endif // XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION
506+
479507
} // namespace xyz
480508

481509
#endif // XYZ_POLYMORPHIC_H_

polymorphic_test.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,6 @@ TEST(PolymorphicTest, TaggedAllocatorsNotEqualMoveConstructFromValueless) {
846846
}
847847

848848
#ifdef XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION
849-
#ifdef XYZ_HAS_STD_TYPE_IDENTITY
850-
#ifdef XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
851849
TEST(PolymorphicTest, TemplateArgumentDeduction) {
852850
xyz::polymorphic p(Derived(4));
853851

@@ -864,15 +862,15 @@ TEST(PolymorphicTest, TemplateArgumentDeductionCopy) {
864862
}
865863

866864
TEST(PolymorphicTest, TemplateArgumentDeductionWithAllocator) {
867-
xyz::TaggedAllocator<int> a(1);
865+
xyz::TaggedAllocator<Derived> a(1);
868866
xyz::polymorphic p(std::allocator_arg, a, Derived(4));
869867

870868
EXPECT_EQ(p->value(), 4);
871869
EXPECT_EQ(p.get_allocator().tag, 1);
872870
}
873871

874872
TEST(PolymorphicTest, TemplateArgumentDeductionWithDeducedAllocatorAndCopy) {
875-
xyz::TaggedAllocator<int> a(1);
873+
xyz::TaggedAllocator<Derived> a(1);
876874
xyz::polymorphic p(std::allocator_arg, a, Derived(4));
877875
xyz::polymorphic pp(std::allocator_arg, 2, p);
878876

@@ -881,16 +879,14 @@ TEST(PolymorphicTest, TemplateArgumentDeductionWithDeducedAllocatorAndCopy) {
881879
}
882880

883881
TEST(PolymorphicTest, TemplateArgumentDeductionWithDeducedAllocatorAndMove) {
884-
xyz::TaggedAllocator<int> a(1);
882+
xyz::TaggedAllocator<Derived> a(1);
885883
xyz::polymorphic p(std::allocator_arg, a, Derived(4));
886884
xyz::polymorphic pp(std::allocator_arg, 2, std::move(p));
887885

888886
EXPECT_EQ(pp->value(), 4);
889887
EXPECT_EQ(pp.get_allocator().tag, 2);
890888
}
891889

892-
#endif // XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
893-
#endif // XYZ_HAS_STD_TYPE_IDENTITY
894890
#endif // XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION
895891

896892
} // namespace

0 commit comments

Comments
 (0)