From 9cef4e91b6acc4b97efea68d2ecaf47ff084bd07 Mon Sep 17 00:00:00 2001 From: Patrick Roberts Date: Sat, 6 Jun 2026 21:50:14 -0500 Subject: [PATCH] Add lifetimebound attribute --- include/beman/any_view/CMakeLists.txt | 1 + include/beman/any_view/any_view.hpp | 15 +++++++++++++++ include/beman/any_view/detail/lifetimebound.hpp | 16 ++++++++++++++++ .../beman/any_view/detail/no_unique_address.hpp | 10 +++++++--- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 include/beman/any_view/detail/lifetimebound.hpp diff --git a/include/beman/any_view/CMakeLists.txt b/include/beman/any_view/CMakeLists.txt index 80deaf8..6f72ae1 100644 --- a/include/beman/any_view/CMakeLists.txt +++ b/include/beman/any_view/CMakeLists.txt @@ -15,6 +15,7 @@ target_sources( detail/default_iterator.hpp detail/default_view.hpp detail/iterator.hpp + detail/lifetimebound.hpp detail/no_unique_address.hpp detail/polymorphic.hpp detail/polymorphic_iterator.hpp diff --git a/include/beman/any_view/any_view.hpp b/include/beman/any_view/any_view.hpp index a6e5bd1..426884b 100644 --- a/include/beman/any_view/any_view.hpp +++ b/include/beman/any_view/any_view.hpp @@ -5,6 +5,7 @@ #include #include +#include #include namespace beman::any_view { @@ -132,6 +133,20 @@ class any_view : public std::ranges::view_interface + requires(not std::ranges::enable_view>) and + ext_any_compatible_range + constexpr any_view(BEMAN_ANY_VIEW_LIFETIMEBOUND RangeT& range) + : poly(adaptor_for>{.view = std::views::all(range)}) { + static_assert(std::ranges::viewable_range, "range must be viewable"); + if constexpr (copyable) { + static_assert(std::copyable>, + "range must be convertible to copyable view if any_view is copyable"); + } + } +#endif // BEMAN_ANY_VIEW_LIFETIMEBOUND + constexpr any_view() noexcept = default; constexpr any_view(const any_view&) diff --git a/include/beman/any_view/detail/lifetimebound.hpp b/include/beman/any_view/detail/lifetimebound.hpp new file mode 100644 index 0000000..674c0d4 --- /dev/null +++ b/include/beman/any_view/detail/lifetimebound.hpp @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef BEMAN_ANY_VIEW_DETAIL_LIFETIMEBOUND_HPP +#define BEMAN_ANY_VIEW_DETAIL_LIFETIMEBOUND_HPP + +#ifndef BEMAN_ANY_VIEW_LIFETIMEBOUND + #ifdef __has_cpp_attribute + #if __has_cpp_attribute(clang::lifetimebound) + #define BEMAN_ANY_VIEW_LIFETIMEBOUND [[clang::lifetimebound]] + #elif __has_cpp_attribute(msvc::lifetimebound) + #define BEMAN_ANY_VIEW_LIFETIMEBOUND [[msvc::lifetimebound]] + #endif + #endif // __has_cpp_attribute +#endif // BEMAN_ANY_VIEW_LIFETIMEBOUND + +#endif // BEMAN_ANY_VIEW_DETAIL_LIFETIMEBOUND_HPP diff --git a/include/beman/any_view/detail/no_unique_address.hpp b/include/beman/any_view/detail/no_unique_address.hpp index 773a738..d55de5e 100644 --- a/include/beman/any_view/detail/no_unique_address.hpp +++ b/include/beman/any_view/detail/no_unique_address.hpp @@ -4,11 +4,15 @@ #define BEMAN_ANY_VIEW_DETAIL_NO_UNIQUE_ADDRESS_HPP #ifndef BEMAN_ANY_VIEW_NO_UNIQUE_ADDRESS - #if _MSC_VER + #ifndef __has_cpp_attribute + #define BEMAN_ANY_VIEW_NO_UNIQUE_ADDRESS + #elif __has_cpp_attribute(msvc::no_unique_address) #define BEMAN_ANY_VIEW_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] - #else + #elif __has_cpp_attribute(no_unique_address) #define BEMAN_ANY_VIEW_NO_UNIQUE_ADDRESS [[no_unique_address]] - #endif // _MSC_VER + #else + #define BEMAN_ANY_VIEW_NO_UNIQUE_ADDRESS + #endif // __has_cpp_attribute #endif // BEMAN_ANY_VIEW_NO_UNIQUE_ADDRESS #endif // BEMAN_ANY_VIEW_DETAIL_NO_UNIQUE_ADDRESS_HPP