Skip to content

Commit 0739328

Browse files
Abseil Teamcopybara-github
authored andcommitted
Add std::pair specializations for IsOwner and IsView
Also fix a typo in the comment. Fixes: #1930 PiperOrigin-RevId: 804996432 Change-Id: Iec326a4e066a885295a09691dd6773847b9a68a3
1 parent 266b1a0 commit 0739328

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

absl/meta/type_traits.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <string>
4141
#include <string_view>
4242
#include <type_traits>
43+
#include <utility>
4344
#include <vector>
4445

4546
#include "absl/base/attributes.h"
@@ -451,7 +452,7 @@ namespace type_traits_internal {
451452

452453
// Detects if a class's definition has declared itself to be an owner by
453454
// declaring
454-
// using absl_internal_is_view = std::true_type;
455+
// using absl_internal_is_view = std::false_type;
455456
// as a member.
456457
// Types that don't want either must either omit this declaration entirely, or
457458
// (if e.g. inheriting from a base class) define the member to something that
@@ -479,6 +480,17 @@ struct IsOwnerImpl<
479480
template <typename T>
480481
struct IsOwner : IsOwnerImpl<T> {};
481482

483+
// This allows incomplete types to be used for associative containers, and also
484+
// expands the set of types we can handle to include std::pair.
485+
template <typename T1, typename T2>
486+
struct IsOwner<std::pair<T1, T2>>
487+
: std::integral_constant<
488+
bool, std::conditional_t<std::is_reference_v<T1>, std::false_type,
489+
IsOwner<std::remove_cv_t<T1>>>::value &&
490+
std::conditional_t<std::is_reference_v<T2>, std::false_type,
491+
IsOwner<std::remove_cv_t<T2>>>::value> {
492+
};
493+
482494
template <typename T, typename Traits, typename Alloc>
483495
struct IsOwner<std::basic_string<T, Traits, Alloc>> : std::true_type {};
484496

@@ -513,6 +525,13 @@ template <typename T>
513525
struct IsView : std::integral_constant<bool, std::is_pointer<T>::value ||
514526
IsViewImpl<T>::value> {};
515527

528+
// This allows incomplete types to be used for associative containers, and also
529+
// expands the set of types we can handle to include std::pair.
530+
template <typename T1, typename T2>
531+
struct IsView<std::pair<T1, T2>>
532+
: std::integral_constant<bool, IsView<std::remove_cv_t<T1>>::value &&
533+
IsView<std::remove_cv_t<T2>>::value> {};
534+
516535
template <typename Char, typename Traits>
517536
struct IsView<std::basic_string_view<Char, Traits>> : std::true_type {};
518537

absl/meta/type_traits_test.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ using IsOwnerAndNotView =
3636
absl::conjunction<absl::type_traits_internal::IsOwner<T>,
3737
absl::negation<absl::type_traits_internal::IsView<T>>>;
3838

39+
static_assert(
40+
IsOwnerAndNotView<std::pair<std::vector<int>, std::string>>::value,
41+
"pair of owners is an owner, not a view");
3942
static_assert(IsOwnerAndNotView<std::vector<int>>::value,
4043
"vector is an owner, not a view");
4144
static_assert(IsOwnerAndNotView<std::string>::value,

0 commit comments

Comments
 (0)