### to_array example Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat An example demonstrating the usage of `boost::compat::to_array` to convert a C-style array into a `std::array`. ```cpp int input [] = {1, 2, 3}; std::array output = boost::compat::to_array(input); assert(( output == std::array{{1, 2, 3}} )); ``` -------------------------------- ### Boost Compat Latch Example Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat This example demonstrates the usage of boost::compat::latch for synchronizing multiple threads. A latch is initialized with the number of threads, and each thread arrives and waits at the latch before proceeding. ```cpp std::ptrdiff_t const num_threads = 16; boost::compat::latch l(num_threads); std::vector threads; for (int i = 0; i < num_threads; ++i) { threads.emplace_back([&l] { // do some preliminary work here... // block until all threads have reached this statement l.arrive_and_wait(); // continue with further work... }); } for (auto& t: threads) { t.join(); } ``` -------------------------------- ### shared_lock Constructor Examples - Basic Usage Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Demonstrates practical usage of shared_lock constructors including default construction, immediate locking, deferred locking, and move semantics. Shows how to acquire and manage shared locks on a shared_mutex. ```cpp #include shared_mutex m; // acquire the lock by calling `m.lock_shared()` // `m.unlock_shared()` is called automatically for us by `guard` now boost::compat::shared_lock guard(m); assert(guard.owns_lock()); ``` -------------------------------- ### Boost.Compat Latch Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Demonstrates the usage of boost::compat::latch for thread synchronization. A latch is initialized with the number of threads, and each thread arrives and waits at the latch. Once all threads have arrived, they are all released to continue execution. This ensures a specific point of synchronization before further processing. ```cpp #include #include #include std::ptrdiff_t const num_threads = 16; bool initialized = false; boost::compat::latch l(num_threads); std::vector threads; for (int i = 0; i < num_threads; ++i) { threads.emplace_back([&l] { // do some preliminary work here... // block until all threads have reached this statement l.arrive_and_wait(); // continue with further work... }); } for (auto& t: threads) { t.join(); } ``` -------------------------------- ### mem_fn Usage Example Source: https://www.boost.org/doc/libs/latest/libs/compat/index Illustrates the usage of the mem_fn utility, showing how it creates a function object to access member functions or objects of a given type. ```cpp template auto mem_fn(M T::* pm) noexcept; // Example usage: struct MyClass { void foo() { /* ... */ } int bar; }; MyClass obj; auto foo_func = mem_fn(&MyClass::foo); foo_func(obj); // Calls obj.foo() auto bar_val = mem_fn(&MyClass::bar); int value = bar_val(obj); // Accesses obj.bar ``` -------------------------------- ### Boost.Compat move_only_function Example Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Provides an example of using boost::compat::move_only_function, demonstrating its ability to wrap a move-only callable object. This contrasts with std::function, highlighting move_only_function's requirement for move construction/assignment rather than copyability. ```cpp struct moveonly { std::unique_ptr p_ = std::make_unique( 1234 ); auto operator()( int x ) noexcept { return *p_ + x; } }; boost::compat::move_only_function f( moveonly{} ); std::cout << f( 4321 ) << std::endl; ``` -------------------------------- ### Boost.Compat mem_fn Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Illustrates the use of boost::compat::mem_fn to create a function object that calls a member function of a class. The example shows how to obtain a mem_fn object for a const member function and invoke it on an instance of the class with arguments. This utility provides a callable wrapper for member access. ```cpp struct X { void f(int a, int b) const noexcept; }; int main() { auto fn = boost::compat::mem_fn(&X::f); X x; fn(x, 1, 2); // calls x.f(1, 2) } ``` -------------------------------- ### Boost.Compat move_only_function Description and Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Introduces ``, implementing C++23's `std::move_only_function`. It wraps callable objects requiring only move semantics, unlike `std::function` which needs copyability. The example demonstrates wrapping a move-only struct with an overloaded call operator. ```cpp #include #include #include struct moveonly { std::unique_ptr p_ = std::make_unique( 1234 ); auto operator()( int x ) noexcept { return *p_ + x; } }; int main() { boost::compat::move_only_function f( moveonly{} ); std::cout << f( 4321 ) << std::endl; return 0; } ``` -------------------------------- ### Integer Sequence Utilities (C++14) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Implements C++14 utilities std::integer_sequence, std::index_sequence, std::make_integer_sequence, std::make_index_sequence, and std::index_sequence_for. These are part of the boost::compat namespace. ```cpp namespace boost { namespace compat { template struct integer_sequence; template using make_integer_sequence = /*...*/; template using index_sequence = /*...*/; template using make_index_sequence = /*...*/; template using index_sequence_for = /*...*/; } // namespace compat } // namespace boost ``` ```cpp template struct integer_sequence {}; ``` ```cpp template using make_integer_sequence = /*...*/; ``` ```cpp template using index_sequence = integer_sequence; ``` ```cpp template using make_index_sequence = make_integer_sequence; ``` ```cpp template using index_sequence_for = make_integer_sequence; ``` -------------------------------- ### boost::compat::shared_lock Constructors Source: https://www.boost.org/doc/libs/latest/libs/compat/index Showcases the various constructors available for boost::compat::shared_lock, including default, locking, deferred, try-to-lock, adopting, and move constructors. ```cpp namespace boost { namespace compat { template class shared_lock { // ... public: // Default constructor shared_lock() noexcept = default; // Locking constructor explicit shared_lock( mutex_type& m ); // Deferred constructor shared_lock( mutex_type& m, std::defer_lock_t ) noexcept; // Try-to-lock constructor shared_lock( mutex_type& m, std::try_lock_t ); // Adopting constructor shared_lock( mutex_type& m, std::adopt_lock_t ); // Move constructor shared_lock( shared_lock&& u ) noexcept; // ... }; } // namespace compat } // namespace boost ``` -------------------------------- ### Boost.Compat function_ref Constructors Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Illustrates the various constructors for boost::compat::function_ref, enabling initialization from function pointers, objects, pointers to member functions, and bound object/pointer combinations. Each constructor has specific preconditions and effects on how the callable is stored and invoked. ```cpp template function_ref(F* fn) noexcept; ``` ```cpp template function_ref(F&& fn) noexcept; ``` ```cpp template function_ref(nontype_t) noexcept; ``` ```cpp template function_ref(nontype_t, U&&) noexcept; ``` ```cpp template function_ref(nontype_t, cv T*) noexcept; ``` ```cpp function_ref(const function_ref&) noexcept = default; ``` -------------------------------- ### Boost.Compat Latch Synopsis (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Provides the class definition and member function signatures for boost::compat::latch. This class acts as a single-use barrier, decrementing a counter and blocking threads until the counter reaches zero. It includes constructors, member functions for countdown, waiting, and a static method to determine the maximum number of waiters. ```cpp namespace boost { namespace compat { class latch { explicit latch(std::ptrdiff_t expected); latch(latch const &) = delete; latch &operator=(latch const &) = delete; ~latch() = default; void count_down(std::ptrdiff_t n = 1); bool try_wait() const noexcept; void wait() const; void arrive_and_wait(std::ptrdiff_t n = 1); static constexpr std::ptrdiff_t max() noexcept; }; } // namespace compat } // namespace boost ``` -------------------------------- ### C++20 std::bind_front implementation using Boost.Compat Source: https://www.boost.org/doc/libs/latest/libs/compat/index Implements the C++20 `std::bind_front` functionality, which creates a function object by binding the first parameters of another function. This is useful for creating specialized function objects. ```cpp struct X { void f(int a, int b) const noexcept; }; int main() { X x; auto fn = boost::compat::bind_front(&X::f, &x); fn(1, 2); // calls x.f(1, 2) } ``` -------------------------------- ### Boost.Compat move_only_function Constructors (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Details the constructors for move_only_function, including default, null, callable object, and in-place construction. It specifies effects and constraints for each constructor type. ```cpp move_only_function() noexcept; ``` ```cpp move_only_function( std::nullptr_t ) noexcept; ``` ```cpp template move_only_function( F&& f ); ``` ```cpp template explicit move_only_function( in_place_type_t, CArgs&& ... args ); ``` ```cpp template explicit move_only_function( in_place_type_t, std::initializer_list il, CArgs&& ... args ); ``` ```cpp move_only_function( move_only_function&& f ) = default; ``` -------------------------------- ### Boost.Compat bind_front Implementation (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Implements the C++20 std::bind_front functionality, which creates a function object by binding leading arguments. It's a limited variant of std::bind. ```cpp struct X { void f(int a, int b) const noexcept; }; int main() { X x; auto fn = boost::compat::bind_front(&X::f, &x); fn(1, 2); // calls x.f(1, 2) } ``` ```cpp namespace boost { namespace compat { template auto bind_front(F&& f, A&&... a); } } ``` ```cpp template auto bind_front(F&& f, A&&... a); ``` -------------------------------- ### boost::compat::shared_lock Basic Usage Source: https://www.boost.org/doc/libs/latest/libs/compat/index Demonstrates the fundamental usage of boost::compat::shared_lock for acquiring and automatically releasing a shared lock on a shared_mutex. ```cpp #include #include #include // ... shared_mutex m; // acquire the lock by calling `m.lock_shared()` // `m.unlock_shared()` is called automatically for us by `guard` now boost::compat::shared_lock guard(m); assert(guard.owns_lock()); // The lock is automatically released when 'guard' goes out of scope. ``` -------------------------------- ### boost::compat::shared_lock Member Functions Source: https://www.boost.org/doc/libs/latest/libs/compat/index Outlines the key member functions of boost::compat::shared_lock, including lock, try_lock, unlock, release, mutex, and owns_lock. ```cpp namespace boost { namespace compat { template class shared_lock { // ... public: void lock(); bool try_lock(); void unlock(); void swap( shared_lock& u ) noexcept; mutex_type* release() noexcept; mutex_type* mutex() const noexcept; bool owns_lock() const noexcept; explicit operator bool() const noexcept; }; } // namespace compat } // namespace boost ``` -------------------------------- ### C++20 std::bind_back implementation using Boost.Compat Source: https://www.boost.org/doc/libs/latest/libs/compat/index Implements the C++20 `std::bind_back` functionality, which creates a function object by binding the last parameters of another function. This is useful for adapting function signatures. ```cpp struct X { void f(int a, int b) const noexcept; }; int main() { auto fn = boost::compat::bind_back(&X::f, 1, 2); X x; fn(x); // calls x.f(1, 2) } ``` -------------------------------- ### Function Pointer Constructor for function_ref (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Constructs a `boost::compat::function_ref` using a raw function pointer. Requires that the provided function pointer `fn` is not null. The constructed `function_ref` will call the provided function pointer when invoked. ```cpp template function_ref(F* fn) noexcept; ``` -------------------------------- ### Boolean Conversion for Boost.Compat Types Source: https://www.boost.org/doc/libs/latest/libs/compat/index Provides an explicit conversion to bool for boost.compat types. Returns true if the object is managing an object, and false if it is empty. No external dependencies are required. ```cpp explicit operator bool() const noexcept; ``` -------------------------------- ### Boost.Compat move_only_function Utility Functions (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Details utility functions for move_only_function, such as null equality checks and swap operations. These functions provide essential comparisons and state management capabilities. ```cpp friend bool operator==( move_only_function const& fn, std::nullptr_t ) noexcept; ``` ```cpp friend bool operator!=( move_only_function const& fn, std::nullptr_t ) noexcept; ``` ```cpp void swap( move_only_function& rhs ) noexcept; ``` ```cpp friend void swap( move_only_function& lhs, move_only_function& rhs ) noexcept; ``` -------------------------------- ### Boost Compat Latch Counter Constructor Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat The constructor for boost::compat::latch that initializes the latch with a specified number of expected threads. Preconditions include the expected count being non-negative and within the maximum limit. ```cpp explicit latch(std::ptrdiff_t expected); ``` -------------------------------- ### Pointer to Member Function Constructor for function_ref (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Constructs a `boost::compat::function_ref` using a pointer to a member function. This overload is selected when `f` is a pointer to a member or member function. The `function_ref` will invoke the member function on an implicitly provided object. ```cpp template function_ref(nontype_t) noexcept; ``` -------------------------------- ### Copy Constructor for function_ref (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index The copy constructor for `boost::compat::function_ref`. As `function_ref` is a TriviallyCopyable type, the default copy constructor is sufficient for creating independent copies. ```cpp function_ref(const function_ref&) noexcept = default; ``` -------------------------------- ### Boost.Compat bind_back Implementation (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Implements the C++20 std::bind_back functionality, which creates a function object by binding trailing arguments. It's a limited variant of std::bind. ```cpp struct X { void f(int a, int b) const noexcept; }; int main() { auto fn = boost::compat::bind_back(&X::f, 1, 2); X x; fn(x); // calls x.f(1, 2) } ``` ```cpp namespace boost { namespace compat { template auto bind_back(F&& f, A&&... a); } } ``` ```cpp template auto bind_back( F&& f, A&&... a ); ``` -------------------------------- ### Invoke Utilities (C++17) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Implements the C++17 function std::invoke, std::invoke_r, and related utilities like invoke_result_t, is_invocable, etc. These functions allow uniform treatment of functions, function objects, and pointers to members. ```cpp namespace boost { namespace compat { template auto invoke(F&& f, A&&... a); template using invoke_result_t = /*...*/; template struct is_invocable; template struct is_nothrow_invocable; template R invoke_r(F&& f, A&&... a); template struct is_invocable_r; template struct is_nothrow_invocable_r; } // namespace compat } // namespace boost ``` ```cpp template auto invoke(F&& f, A&&... a) noexcept(/*...*/) -> /*...*/; ``` ```cpp template using invoke_result_t = decltype( invoke(std::declval(), std::declval()...) ); ``` ```cpp template struct is_invocable: public /*...*/; ``` ```cpp template struct is_nothrow_invocable: public /*...*/; ``` ```cpp struct X { void f(int a, int b) const noexcept; }; int main() { X x; boost::compat::invoke(&X::f, x, 1, 2); // calls x.f(1, 2) } ``` -------------------------------- ### to_array C++20 std::to_array implementation (move) Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Creates a `std::array` from a C-style array using move semantics. It is a C++20 compatible implementation provided by Boost.Compat. ```cpp template constexpr std::array, N> to_array(T (&&a)[N]); ``` -------------------------------- ### Bound Pointer Constructor for function_ref (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Constructs a `boost::compat::function_ref` with a pointer to a member function and a pointer to an object. This overload allows binding a member function to an object instance via a pointer, enabling its invocation on that object. ```cpp template function_ref(nontype_t, cv T*) noexcept; ``` -------------------------------- ### shared_lock RAII Wrapper - Portable C++14 Shared Mutex Locking Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat RAII wrapper class template that manages locking and unlocking of a SharedLockable mutex. Provides multiple constructors for different locking strategies (immediate lock, deferred, try-lock, adopt-lock) and supports move semantics. Automatically calls unlock_shared() on destruction when lock is held. ```cpp namespace boost { namespace compat { template class shared_lock; template void swap( shared_lock& x, shared_lock& y ) noexcept; template class shared_lock { using mutex_type = Mutex; shared_lock() noexcept = default; explicit shared_lock( mutex_type& m ); shared_lock( mutex_type& m, std::defer_lock_t ) noexcept; shared_lock( mutex_type& m, std::try_lock_t ); shared_lock( mutex_type& m, std::adopt_lock_t ); ~shared_lock(); shared_lock( const shared_lock& ) = delete; shared_lock& operator=( const shared_lock& ) = delete; shared_lock( shared_lock&& u ) noexcept; shared_lock& operator=( shared_lock&& u ) noexcept; void lock(); bool try_lock(); void unlock(); void swap( shared_lock& u ) noexcept; mutex_type* release() noexcept; mutex_type* mutex() const noexcept; bool owns_lock() const noexcept; explicit operator bool() const noexcept; }; } // namespace compat } // namespace boost ``` -------------------------------- ### Invoke Function Utilities (C++17) Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Implements C++17 std::invoke and related utilities like invoke_result_t, is_invocable, etc. These provide a uniform way to call functions, member functions, and function objects, handling pointers to members correctly. ```cpp namespace boost { namespace compat { template auto invoke(F&& f, A&&... a); template using invoke_result_t = /*...*/; template struct is_invocable; template struct is_nothrow_invocable; template R invoke_r(F&& f, A&&... a); template struct is_invocable_r; template struct is_nothrow_invocable_r; } // namespace compat } // namespace boost ``` ```cpp struct X { void f(int a, int b) const noexcept; }; int main() { X x; boost::compat::invoke(&X::f, x, 1, 2); // calls x.f(1, 2) } ``` ```cpp template auto invoke(F&& f, A&&... a) noexcept(/*...*/) -> /*...*/; ``` ```cpp template using invoke_result_t = decltype( invoke(std::declval(), std::declval()...) ); ``` ```cpp template struct is_invocable: public /*...*/; ``` ```cpp template struct is_nothrow_invocable: public /*...*/; ``` -------------------------------- ### Null Constructor for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Constructs an empty move_only_function using a null pointer. This is equivalent to the default constructor and signifies that the function object is empty. ```cpp move_only_function( std::nullptr_t ) noexcept; ``` -------------------------------- ### Boost.Compat mem_fn Synopsis (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Defines the boost::compat::mem_fn function template, which returns a function object capable of invoking a member of a given type. It takes a pointer to a member (function or data) and returns an object that can be called with an object instance and the necessary arguments, mimicking a regular function call. ```cpp namespace boost { namespace compat { template auto mem_fn(M T::* pm) noexcept; } // namespace compat } // namespace boost ``` -------------------------------- ### C++26 std::function_ref implementation using Boost.Compat Source: https://www.boost.org/doc/libs/latest/libs/compat/index Provides a lightweight, polymorphic function wrapper similar to C++26's `std::function_ref`. It does not own the callable object and avoids allocations, making it suitable for higher-order functions and performance-critical code. ```cpp #include #include #include int add(int x, int y) noexcept { return x * 10 + y; } auto add2 = [](int x, int y) { return x * 100 + y; }; int main() { std::vector> fns; fns.push_back({add}); fns.push_back({add2}); for (auto fn : fns) { std::cout << fn(1, 2) << std::endl; } } ``` -------------------------------- ### to_array C++20 std::to_array implementation (copy) Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Creates a `std::array` from a C-style array using copy semantics. It is a C++20 compatible implementation provided by Boost.Compat. ```cpp template constexpr std::array, N> to_array(T (&a)[N]); ``` -------------------------------- ### Boost.Compat move_only_function Class Definition (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Defines the move_only_function class template for creating move-only callable wrappers. It supports various constructors for empty states, callable objects, and in-place construction. It also includes move semantics and comparison operators. ```cpp namespace boost { namespace compat { template class move_only_function; template< class T > struct in_place_type_t { explicit in_place_type_t() = default; }; template class move_only_function { move_only_function() noexcept; move_only_function( std::nullptr_t ) noexcept; template move_only_function( F&& f ); template explicit move_only_function( in_place_type_t, CArgs&& ... args ); template explicit move_only_function( in_place_type_t, std::initializer_list il, CArgs&& ... args ); move_only_function( move_only_function const& ) = delete; move_only_function( move_only_function&& ) = default; ~move_only_function() = default; move_only_function& operator=( move_only_function&& rhs ); move_only_function& operator=( std::nullptr_t ) noexcept; template move_only_function& operator=( F&& f ); friend bool operator==( move_only_function const& fn, std::nullptr_t ) noexcept; friend bool operator!=( move_only_function const& fn, std::nullptr_t ) noexcept; void swap( move_only_function& rhs ) noexcept; friend void swap( move_only_function& lhs, move_only_function& rhs ) noexcept; explicit operator bool() const noexcept; R operator()( Args... args ) /* cv */ /* ref */ noexcept( /* noex */ ); }; } // namespace compat } // namespace boost ``` -------------------------------- ### Boost Compat Latch Arrive and Wait Member Function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat The arrive_and_wait member function of boost::compat::latch. It decrements the internal counter and blocks the current thread if the counter is still non-zero. Preconditions ensure 'n' does not exceed the current counter value. ```cpp void arrive_and_wait(std::ptrdiff_t n = 1); ``` -------------------------------- ### mem_fn - Create Function Object from Member Pointer Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Template function that returns a function object wrapping a member function or data member pointer. The resulting function object automatically handles dereferencing and calling the member, supporting both direct objects and pointers with appropriate member access syntax. ```cpp template auto mem_fn(M T::* pm) noexcept; ``` -------------------------------- ### Default Constructor for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Constructs an empty move_only_function that does not manage any object. This is the default state of the object. ```cpp move_only_function() noexcept; ``` -------------------------------- ### Move Constructor for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Constructs a new move_only_function by transferring ownership of the managed object from another move_only_function `f`. After the operation, `f` becomes empty. ```cpp move_only_function( move_only_function&& f ) = default; ``` -------------------------------- ### shared_lock free function swap Source: https://www.boost.org/doc/libs/latest/libs/compat/index Free function template that swaps the internal state of two shared_lock instances by calling the member swap function. This operation is noexcept. ```cpp template void swap( shared_lock& x, shared_lock& y ) noexcept; ``` -------------------------------- ### boost::compat::shared_lock::lock Method Source: https://www.boost.org/doc/libs/latest/libs/compat/index Details the `lock()` member function of boost::compat::shared_lock, which acquires a shared lock on the associated mutex. ```cpp namespace boost { namespace compat { template class shared_lock { // ... public: void lock(); // ... }; } // namespace compat } // namespace boost // Effects: Calls mutex()->lock_shared(). // Postconditions: owns_lock() == true. // Throws: Any exception caused by mutex()->lock_shared(). std::system_error when mutex() == nullptr (with std::errc::operation_not_permitted) or owns_lock() == true (with std::errc::resource_deadlock_would_occur). ``` -------------------------------- ### Bound Object Constructor for function_ref (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Constructs a `boost::compat::function_ref` with a pointer to a member function and a bound object reference. This allows the `function_ref` to call a member function on a specific object instance. It is constrained to not use rvalue references for the bound object. ```cpp template function_ref(nontype_t, U&&) noexcept; ``` -------------------------------- ### Swap Functions for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Exchanges the managed objects between two move_only_function objects. This operation is noexcept and efficient. ```cpp void swap( move_only_function& rhs ) noexcept; friend void swap( move_only_function& lhs, move_only_function& rhs ) noexcept; ``` -------------------------------- ### Boost.Compat move_only_function Assignment Operators (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Covers the assignment operators for move_only_function, including move assignment, null assignment, and callable assignment. These operators manage the internal state and the callable object it holds. ```cpp move_only_function& operator=( move_only_function&& rhs ); ``` ```cpp move_only_function& operator=( std::nullptr_t ) noexcept; ``` ```cpp template move_only_function& operator=( F&& f ); ``` -------------------------------- ### boost::compat::to_array C++20 compatible conversion Source: https://www.boost.org/doc/libs/latest/libs/compat/index Converts a C-style array to a std::array by performing element-wise copy or move initialization. This is a portable implementation of C++20 std::to_array that works with both lvalue and rvalue array references. ```cpp int input [] = {1, 2, 3}; std::array output = boost::compat::to_array(input); assert(( output == std::array{{1, 2, 3}} )); ``` ```cpp template constexpr std::array, N> to_array(T (&&a)[N]); ``` ```cpp template constexpr std::array, N> to_array(T (&a)[N]); ``` -------------------------------- ### Boost.Compat move_only_function Synopsis Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Defines the structure and primary members of the move_only_function class. It supports move semantics and can hold any callable type. ```cpp namespace boost { namespace compat { template class move_only_function; template< class T > struct in_place_type_t { explicit in_place_type_t() = default; }; template class move_only_function { move_only_function() noexcept; move_only_function( std::nullptr_t ) noexcept; template move_only_function( F&& f ); template explicit move_only_function( in_place_type_t, CArgs&& ... args ); template explicit move_only_function( in_place_type_t, std::initializer_list il, CArgs&& ... args ); move_only_function( move_only_function const& ) = delete; move_only_function( move_only_function&& ) = default; ~move_only_function() = default; move_only_function& operator=( move_only_function&& rhs ); move_only_function& operator=( std::nullptr_t ) noexcept; template move_only_function& operator=( F&& f ); friend bool operator==( move_only_function const& fn, std::nullptr_t ) noexcept; friend bool operator!=( move_only_function const& fn, std::nullptr_t ) noexcept; void swap( move_only_function& rhs ) noexcept; friend void swap( move_only_function& lhs, move_only_function& rhs ) noexcept; explicit operator bool() const noexcept; R operator()( Args... args ) noexcept; }; } // namespace compat } // namespace boost ``` -------------------------------- ### Invoke Result (invoke_r) Function Signature Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat The invoke_r function template is used to invoke a callable object and return a result of a specified type R. It handles conversions and noexcept specifications. This is a C++20 concept implementation. ```cpp template R invoke_r(F&& f, A&&... a) noexcept(/*...*/); ``` -------------------------------- ### Null Equality Operators for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Compares a move_only_function with a null pointer to determine if it currently holds a managed object. `operator==` returns true if empty, `operator!=` returns true if not empty. ```cpp friend bool operator==( move_only_function const& fn, std::nullptr_t ) noexcept; friend bool operator!=( move_only_function const& fn, std::nullptr_t ) noexcept; ``` -------------------------------- ### Object Constructor for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Constructs a move_only_function by taking ownership of a callable object `f`. It stores a decay-ed version of `f` internally. If `f` is an empty function pointer or similar, the resulting move_only_function will be empty. ```cpp template move_only_function( F&& f ); ``` -------------------------------- ### Invoke with Return Type Conversion (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index The invoke_r function template allows invoking a callable object with specified arguments and casting the result to a target return type R. It handles void return types and performs implicit conversions for non-void types. Constraints ensure the callable is invocable and its result is convertible to R. The noexcept specification mirrors that of the underlying invocation. ```cpp template R invoke_r(F&& f, A&&... a) noexcept(/*...*/); // Returns: // * static_cast(invoke(std::forward(f), std::forward(a)…​)), when `R` is (possibly cv-qualified) `void`; // * invoke(std::forward(f), std::forward(a)…​), implicitly converted to `R`, otherwise. // Constraints: // `is_invocable::value` must be `true` and, if `R` is not cv `void`, `std::is_convertible, R>::value` must be `true`. // Remarks: // The `noexcept` clause is `noexcept(noexcept(static_cast(invoke(std::forward(f), std::forward(a)…​))))`. ``` -------------------------------- ### Boost.Compat function_ref Implementation (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Implements the C++26 std::function_ref class, a lightweight polymorphic function wrapper. It does not own the callable object and avoids allocations, supporting various const and noexcept combinations. ```cpp int add(int x, int y) noexcept { return x * 10 + y; } auto add2 = [](int x, int y) { return x * 100 + y; }; std::vector> fns; fns.push_back({add}); fns.push_back({add2}); for (auto fn : fns) { std::cout << fn(1, 2) << std::endl; } ``` -------------------------------- ### shared_lock mutex observer Source: https://www.boost.org/doc/libs/latest/libs/compat/index Returns the internal mutex pointer associated with this shared_lock instance. Returns either nullptr or the address of the mutex object. ```cpp mutex_type* mutex() const noexcept; ``` -------------------------------- ### Boost.Compat move_only_function Invocation (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Describes the invocation mechanism for move_only_function using the overloaded function call operator. This allows the stored callable object to be invoked with specified arguments. ```cpp R operator()( Args... args ) /* cv */ /* ref */ noexcept( /* noex */ ); ``` -------------------------------- ### Call Operator for function_ref (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index The call operator for `boost::compat::function_ref`. This operator invokes the underlying callable object, forwarding any supplied arguments. The `noexcept` specification is determined by the `noex` template parameter. ```cpp R operator()(ArgTypes...) const noexcept(noex); ``` -------------------------------- ### Callable Assignment for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Assigns a new callable object `f` to the move_only_function. The existing managed object is released, and the new object `f` is moved into place. ```cpp template move_only_function& operator=( F&& f ); ``` -------------------------------- ### shared_lock try_lock method Source: https://www.boost.org/doc/libs/latest/libs/compat/index Attempts to acquire a shared lock on the associated mutex without blocking. Returns a boolean indicating success. Throws std::system_error if the mutex is null or if the lock is already owned. ```cpp bool try_lock(); ``` -------------------------------- ### Object Constructor for function_ref (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Constructs a `boost::compat::function_ref` by storing a reference to a callable object. This overload is specifically excluded for pointer-to-member types. It allows `function_ref` to wrap non-pointer callables like lambdas or function objects. ```cpp template function_ref(F&& fn) noexcept; ``` -------------------------------- ### In-Place Constructor for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Constructs a move_only_function by directly creating an object of type `T` within the function object, using the provided arguments `CArgs`. This avoids unnecessary copies or moves. ```cpp template explicit move_only_function( in_place_type_t, CArgs&& ... args ); ``` -------------------------------- ### boost::compat type traits aliases Source: https://www.boost.org/doc/libs/latest/libs/compat/index Type trait template aliases providing C++14+ convenience names for standard type transformations. Includes remove_const_t, remove_cv_t, remove_reference_t, remove_cvref_t, decay_t, enable_if_t, conditional_t, and void_t. ```cpp template using remove_const_t = typename std::remove_const::type; template using remove_cv_t = typename std::remove_cv::type; template using remove_reference_t = typename std::remove_reference::type; template using remove_cvref_t = remove_cv_t>; template using decay_t = typename std::decay::type; template using enable_if_t = typename std::enable_if::type; template using conditional_t = typename std::conditional::type; template using void_t = void; ``` -------------------------------- ### shared_lock swap member function Source: https://www.boost.org/doc/libs/latest/libs/compat/index Exchanges the internal state (mutex pointer and lock ownership) between this shared_lock instance and another. This operation is noexcept and does not throw exceptions. ```cpp void swap( shared_lock& u ) noexcept; ``` -------------------------------- ### Boost.Compat function_ref Class Definition (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index Defines the `boost::compat::function_ref` template class, a non-owning reference to a callable type. It supports various callable types including function pointers, lambdas, and member functions, with support for const and noexcept qualifiers. ```cpp namespace boost { namespace compat { template struct function_ref; // cv is either `const` or empty // noex is either `true` or `false` template class function_ref { public: template function_ref(F*) noexcept; template function_ref(F&&) noexcept; template function_ref(nontype_t) noexcept; template function_ref(nontype_t, U&&) noexcept; template function_ref(nontype_t, cv T*) noexcept; function_ref(const function_ref&) noexcept = default; function_ref& operator=(const function_ref&) noexcept = default; template function_ref& operator=(T) = delete; R operator()(ArgTypes...) const noexcept(noex); }; } // namespace compat } // namespace boost ``` -------------------------------- ### List In-Place Constructor for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Constructs a move_only_function by directly creating an object of type `T` within the function object, using an initializer list `il` and other arguments `CArgs`. This is useful for types that are constructed from initializer lists. ```cpp template explicit move_only_function( in_place_type_t, std::initializer_list il, CArgs&& ... args ); ``` -------------------------------- ### shared_lock::lock - Acquire Shared Lock Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Member function that acquires a shared lock on the managed mutex by calling lock_shared(). Sets owns_lock() to true after successful acquisition. Throws std::system_error if mutex is null or lock is already held. ```cpp void lock(); ``` -------------------------------- ### Copy Assignment for function_ref (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index The copy assignment operator for `boost::compat::function_ref`. Being a TriviallyCopyable type, the default copy assignment is used. Additionally, assignment from arbitrary types `T` is explicitly deleted to prevent unintended conversions. ```cpp function_ref& operator=(const function_ref&) noexcept = default; template function_ref& operator=(T) = delete; ``` -------------------------------- ### Null Assignment for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Assigns a null pointer to the move_only_function, effectively clearing it. If the function object was managing an object, that object is released. ```cpp move_only_function& operator=( std::nullptr_t ) noexcept; ``` -------------------------------- ### Move Assignment for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Assigns the managed object from another move_only_function `rhs` to the current object. Ownership is transferred, and `rhs` becomes empty after the assignment. ```cpp move_only_function& operator=( move_only_function&& rhs ); ``` -------------------------------- ### Invocation Operator for move_only_function Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat Invokes the managed callable object with the provided arguments. The `noexcept` specification depends on the managed callable's own exception specification. ```cpp R operator()( Args... args ) noexcept; ``` -------------------------------- ### boost::compat::shared_lock Destructor Source: https://www.boost.org/doc/libs/latest/libs/compat/index Explains the behavior of the destructor for boost::compat::shared_lock, which automatically unlocks the mutex if the lock is currently held. ```cpp namespace boost { namespace compat { template class shared_lock { // ... public: ~shared_lock(); // ... }; } // namespace compat } // namespace boost ``` -------------------------------- ### shared_lock unlock method Source: https://www.boost.org/doc/libs/latest/libs/compat/index Releases the shared lock on the associated mutex. Sets owns_lock to false. Throws std::system_error if the lock is not currently owned. ```cpp void unlock(); ``` -------------------------------- ### Type Traits: enable_if_t Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat A type alias for `std::enable_if::type`, used for SFINAE to conditionally enable function overloads or template instantiations. ```cpp template using enable_if_t = typename std::enable_if::type; ``` -------------------------------- ### boost::compat::shared_lock Assignment Operators Source: https://www.boost.org/doc/libs/latest/libs/compat/index Details the move assignment operator for boost::compat::shared_lock. Copy assignment is deleted as the lock is not copyable. ```cpp namespace boost { namespace compat { template class shared_lock { // ... public: shared_lock( const shared_lock& ) = delete; // Not copyable shared_lock& operator=( const shared_lock& ) = delete; // Not copyable shared_lock& operator=( shared_lock&& u ) noexcept; // ... }; } // namespace compat } // namespace boost ``` -------------------------------- ### Type Traits: void_t Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat A type alias for `void`, often used in SFINAE contexts to check for the validity of expressions. ```cpp template using void_t = void; ``` -------------------------------- ### Check Nothrow Invocation with Result Type (is_nothrow_invocable_r) Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat The is_nothrow_invocable_r struct template determines if a callable type F, when invoked with arguments A..., can produce a result convertible to R without throwing exceptions. It relies on is_invocable_r and the noexcept specifier of invoke_r. ```cpp template struct is_nothrow_invocable: public /*...*/; ``` -------------------------------- ### shared_lock release method Source: https://www.boost.org/doc/libs/latest/libs/compat/index Releases ownership of the associated mutex without unlocking it. Returns the previous mutex pointer and sets the internal mutex pointer to null. ```cpp mutex_type* release() noexcept; ``` -------------------------------- ### Check Invocation with Result Type (is_invocable_r) Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat The is_invocable_r struct template checks if a callable type F, when invoked with arguments A..., can produce a result convertible to R. It inherits from std::true_type if the invocation is valid, otherwise std::false_type. ```cpp template struct is_invocable: public /*...*/; ``` -------------------------------- ### Type Traits: conditional_t Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat A type alias for `std::conditional::type`, which selects one of two types based on a compile-time boolean condition. ```cpp template using conditional_t = typename std::conditional::type; ``` -------------------------------- ### Type Traits: remove_cvref_t Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat A type alias for `remove_cv_t>`, simplifying the removal of const, volatile, and reference qualifications from a type. ```cpp template using remove_cvref_t = remove_cv_t>; ``` -------------------------------- ### Type Traits: remove_cv_t Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat A type alias for `std::remove_cv::type`, providing a convenient way to strip const and volatile qualifications from a type. ```cpp template using remove_cv_t = typename std::remove_cv::type; ``` -------------------------------- ### shared_lock owns_lock observer Source: https://www.boost.org/doc/libs/latest/libs/compat/index Returns a boolean indicating whether the current shared_lock instance owns a lock on the associated mutex. ```cpp bool owns_lock() const noexcept; ``` -------------------------------- ### Check Nothrow Invocability with Return Type (C++) Source: https://www.boost.org/doc/libs/latest/libs/compat/index The is_nothrow_invocable_r type trait determines if a callable object F can be invoked with arguments A... without throwing exceptions, and if the result is convertible to R. If is_invocable_r is false, it defaults to std::false_type. Otherwise, it evaluates to std::integral_constant(std::declval(), std::declval()…​))>, checking the noexcept specification of the invocation. ```cpp template struct is_nothrow_invocable_r: public /*...*/; // The base class of `is_nothrow_invocable` is `std::false_type` when `is_invocable_r::value` is `false`, `std::integral_constant(std::declval(), std::declval()…​))>` otherwise. ``` -------------------------------- ### Type Traits: remove_const_t Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat A type alias for `std::remove_const::type`, providing a convenient way to strip const qualification from a type. ```cpp template using remove_const_t = typename std::remove_const::type; ``` -------------------------------- ### Type Traits: remove_reference_t Source: https://www.boost.org/doc/libs/latest/libs/compat/doc/html/compat A type alias for `std::remove_reference::type`, providing a convenient way to remove reference qualifications from a type. ```cpp template using remove_reference_t = typename std::remove_reference::type; ```