Add std::pair specializations for IsOwner and IsView

Also fix a typo in the comment.

Fixes: #1930
PiperOrigin-RevId: 804996432
Change-Id: Iec326a4e066a885295a09691dd6773847b9a68a3
This commit is contained in:
Abseil Team
2025-09-09 11:34:35 -07:00
committed by Copybara-Service
parent 266b1a0bf8
commit 0739328652
2 changed files with 23 additions and 1 deletions

View File

@@ -40,6 +40,7 @@
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include "absl/base/attributes.h"
@@ -451,7 +452,7 @@ namespace type_traits_internal {
// Detects if a class's definition has declared itself to be an owner by
// declaring
// using absl_internal_is_view = std::true_type;
// using absl_internal_is_view = std::false_type;
// as a member.
// Types that don't want either must either omit this declaration entirely, or
// (if e.g. inheriting from a base class) define the member to something that
@@ -479,6 +480,17 @@ struct IsOwnerImpl<
template <typename T>
struct IsOwner : IsOwnerImpl<T> {};
// This allows incomplete types to be used for associative containers, and also
// expands the set of types we can handle to include std::pair.
template <typename T1, typename T2>
struct IsOwner<std::pair<T1, T2>>
: std::integral_constant<
bool, std::conditional_t<std::is_reference_v<T1>, std::false_type,
IsOwner<std::remove_cv_t<T1>>>::value &&
std::conditional_t<std::is_reference_v<T2>, std::false_type,
IsOwner<std::remove_cv_t<T2>>>::value> {
};
template <typename T, typename Traits, typename Alloc>
struct IsOwner<std::basic_string<T, Traits, Alloc>> : std::true_type {};
@@ -513,6 +525,13 @@ template <typename T>
struct IsView : std::integral_constant<bool, std::is_pointer<T>::value ||
IsViewImpl<T>::value> {};
// This allows incomplete types to be used for associative containers, and also
// expands the set of types we can handle to include std::pair.
template <typename T1, typename T2>
struct IsView<std::pair<T1, T2>>
: std::integral_constant<bool, IsView<std::remove_cv_t<T1>>::value &&
IsView<std::remove_cv_t<T2>>::value> {};
template <typename Char, typename Traits>
struct IsView<std::basic_string_view<Char, Traits>> : std::true_type {};

View File

@@ -36,6 +36,9 @@ using IsOwnerAndNotView =
absl::conjunction<absl::type_traits_internal::IsOwner<T>,
absl::negation<absl::type_traits_internal::IsView<T>>>;
static_assert(
IsOwnerAndNotView<std::pair<std::vector<int>, std::string>>::value,
"pair of owners is an owner, not a view");
static_assert(IsOwnerAndNotView<std::vector<int>>::value,
"vector is an owner, not a view");
static_assert(IsOwnerAndNotView<std::string>::value,