Skip to content

Commit 4554ce7

Browse files
committed
Just testing
1 parent af5477f commit 4554ce7

File tree

8 files changed

+104
-48
lines changed

8 files changed

+104
-48
lines changed

BUILD.bazel

Lines changed: 4 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,8 +92,9 @@ 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"],
97+
deps = ["feature_check"],
9798
)
9899

99100
cc_test(
@@ -117,6 +118,7 @@ cc_library(
117118
copts = ["-Iexternal/value_types/"],
118119
defines = ["XYZ_POLYMORPHIC_CXX_14"],
119120
visibility = ["//visibility:public"],
121+
deps = ["feature_check"],
120122
)
121123

122124
cc_test(

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: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ 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 {
41+
using type = T;
42+
};
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+
3448
#ifndef XYZ_UNREACHABLE_DEFINED
3549
#define XYZ_UNREACHABLE_DEFINED
3650

@@ -182,7 +196,7 @@ class indirect {
182196
p_ = construct_from(alloc_, ilist, std::forward<Us>(us)...);
183197
}
184198

185-
constexpr indirect(std::allocator_arg_t, const std::type_identity_t<A>& alloc,
199+
constexpr indirect(std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
186200
const indirect& other)
187201
: alloc_(alloc) {
188202
static_assert(std::copy_constructible<T>);
@@ -195,7 +209,7 @@ class indirect {
195209
}
196210

197211
constexpr indirect(
198-
std::allocator_arg_t, const std::type_identity_t<A>& alloc,
212+
std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
199213
indirect&& other) noexcept(allocator_traits::is_always_equal::value)
200214
: p_(nullptr), alloc_(alloc) {
201215
static_assert(std::move_constructible<T>);
@@ -465,16 +479,12 @@ concept is_hashable = requires(T t) { std::hash<T>{}(t); };
465479
template <typename Value>
466480
indirect(Value) -> indirect<Value>;
467481

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
474482
template <typename Alloc, typename Value>
475-
indirect(std::allocator_arg_t, std::type_identity_t<Alloc>,
476-
indirect<Value, Alloc>) -> indirect<Value, Alloc>;
477-
#endif // XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
483+
indirect(std::allocator_arg_t, Alloc, Value) -> indirect<Value, Alloc>;
484+
485+
template <typename Alloc, typename Alloc2, typename Value>
486+
indirect(std::allocator_arg_t, Alloc2, indirect<Value, Alloc>)
487+
-> indirect<Value, Alloc>;
478488

479489
} // namespace xyz
480490

indirect_cxx14.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ 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 {
46+
using type = T;
47+
};
48+
template <class T>
49+
using type_identity_t = typename type_identity<T>::type;
50+
#endif // XYZ_HAS_STD_TYPE_IDENTITY
51+
#endif // XYZ_TYPE_IDENTITY_DEFINED
52+
3953
#ifndef XYZ_UNREACHABLE_DEFINED
4054
#define XYZ_UNREACHABLE_DEFINED
4155

@@ -575,8 +589,11 @@ template <typename Value>
575589
indirect(Value) -> indirect<Value>;
576590

577591
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>>;
592+
indirect(std::allocator_arg_t, Alloc, Value) -> indirect<Value, Alloc>;
593+
594+
template <typename Alloc, typename Alloc2, typename Value>
595+
indirect(std::allocator_arg_t, Alloc2, indirect<Value, Alloc>)
596+
-> indirect<Value, Alloc>;
580597
#endif // XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION
581598

582599
} // 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: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ 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 {
43+
using type = T;
44+
};
45+
template <class T>
46+
using type_identity_t = typename type_identity<T>::type;
47+
#endif // XYZ_HAS_STD_TYPE_IDENTITY
48+
#endif // XYZ_TYPE_IDENTITY_DEFINED
49+
3650
#ifndef XYZ_UNREACHABLE_DEFINED
3751
#define XYZ_UNREACHABLE_DEFINED
3852

@@ -119,12 +133,6 @@ class direct_control_block final : public control_block<T, A> {
119133
template <class T, class A>
120134
class polymorphic;
121135

122-
template <class>
123-
inline constexpr bool is_polymorphic_v = false;
124-
125-
template <class T, class A>
126-
inline constexpr bool is_polymorphic_v<polymorphic<T, A>> = true;
127-
128136
template <class T, class A = std::allocator<T>>
129137
class polymorphic {
130138
using cblock_t = detail::control_block<T, A>;
@@ -251,7 +259,7 @@ class polymorphic {
251259
}
252260

253261
constexpr polymorphic(std::allocator_arg_t,
254-
const std::type_identity_t<A>& alloc,
262+
const xyz::type_identity_t<A>& alloc,
255263
const polymorphic& other)
256264
: alloc_(alloc) {
257265
if (!other.valueless_after_move()) {
@@ -262,7 +270,7 @@ class polymorphic {
262270
}
263271

264272
constexpr polymorphic(
265-
std::allocator_arg_t, const std::type_identity_t<A>& alloc,
273+
std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
266274
polymorphic&& other) noexcept(allocator_traits::is_always_equal::value)
267275
: alloc_(alloc) {
268276
if constexpr (allocator_traits::is_always_equal::value) {
@@ -414,19 +422,17 @@ class polymorphic {
414422
}
415423
}
416424
};
417-
#ifdef XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
425+
418426
template <typename Value>
419427
polymorphic(Value) -> polymorphic<Value>;
420428

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-
426429
template <typename Alloc, typename Value>
427-
polymorphic(std::allocator_arg_t, std::type_identity_t<Alloc>,
428-
polymorphic<Value, Alloc>) -> polymorphic<Value, Alloc>;
429-
#endif // XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
430+
polymorphic(std::allocator_arg_t, Alloc, Value) -> polymorphic<Value, Alloc>;
431+
432+
template <typename Alloc, typename Alloc2, typename Value>
433+
polymorphic(std::allocator_arg_t, Alloc2, polymorphic<Value, Alloc>)
434+
-> polymorphic<Value, Alloc>;
435+
430436
} // namespace xyz
431437

432438
#endif // XYZ_POLYMORPHIC_H_

polymorphic_cxx14.h

Lines changed: 33 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,22 @@ 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 {
53+
using type = T;
54+
};
55+
template <class T>
56+
using type_identity_t = typename type_identity<T>::type;
57+
#endif // XYZ_HAS_STD_TYPE_IDENTITY
58+
} // namespace xyz
59+
#endif // XYZ_TYPE_IDENTITY_DEFINED
60+
4361
#ifndef XYZ_UNREACHABLE_DEFINED
4462
#define XYZ_UNREACHABLE_DEFINED
4563

@@ -319,7 +337,8 @@ class polymorphic : private detail::empty_base_optimization<A> {
319337
typename std::remove_reference<U>::type>::type>{},
320338
std::forward<U>(u)) {}
321339

322-
polymorphic(std::allocator_arg_t, const A& alloc, const polymorphic& other)
340+
polymorphic(std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
341+
const polymorphic& other)
323342
: alloc_base(alloc) {
324343
if (!other.valueless_after_move()) {
325344
cb_ = other.cb_->clone(alloc_base::get());
@@ -335,7 +354,7 @@ class polymorphic : private detail::empty_base_optimization<A> {
335354
other) {}
336355

337356
polymorphic(
338-
std::allocator_arg_t, const A& alloc,
357+
std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
339358
polymorphic&& other) noexcept(allocator_traits::is_always_equal::value)
340359
: alloc_base(alloc) {
341360
if (allocator_traits::propagate_on_container_copy_assignment::value) {
@@ -476,6 +495,18 @@ class polymorphic : private detail::empty_base_optimization<A> {
476495
}
477496
};
478497

498+
#ifdef XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION
499+
template <typename Value>
500+
polymorphic(Value) -> polymorphic<Value>;
501+
502+
template <typename Alloc, typename Value>
503+
polymorphic(std::allocator_arg_t, Alloc, Value) -> polymorphic<Value, Alloc>;
504+
505+
template <typename Alloc, typename Alloc2, typename Value>
506+
polymorphic(std::allocator_arg_t, Alloc2, polymorphic<Value, Alloc>)
507+
-> polymorphic<Value, Alloc>;
508+
#endif // XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION
509+
479510
} // namespace xyz
480511

481512
#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)