Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Assignable wrapper(C++20)

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
      Helper items
      copyable-box
      movable-box
      (until C++23)  (C++23)


       
      template<class T>

          requiresstd::copy_constructible<T>&&std::is_object_v<T>

      class/*copyable-box*/;
      (since C++20)
      (until C++23)
      (exposition only*)
      template<class T>

          requiresstd::move_constructible<T>&&std::is_object_v<T>

      class/*movable-box*/;
      (since C++23)
      (exposition only*)

      ranges::single_view,ranges::repeat_view,(since C++23) and range adaptors that store an invocable object are specified in terms of an exposition-only class templatecopyable-box(until C++23)movable-box(since C++23). The name shown here is for exposition purposes only.

      The wrapper behaves exactly likestd::optional<T>, except that the default constructor, copy assignment operator, and move assignment operator are (conditionally) different from those ofstd::optional, which augmentsT with assignability when needed and makes it always satisfycopyableormovable(since C++23).

      IfT is alreadycopyable, or bothstd::is_nothrow_move_constructible_v<T> andstd::is_nothrow_copy_constructible_v<T> aretrue,/*copyable-box*/<T> may store only aT object, because it always contains a value.

      (until C++23)

      IfT

      /*movable-box*/<T> may store only aT object, because it always contains a value.

      (since C++23)

      Contents

      [edit]Template parameters

      T - the type of the contained value, must be an object type that modelscopy_constructible(until C++23)move_constructible(since C++23)

      [edit]Member functions

      Default constructor

      constexpr/*copyable-box*/()noexcept(std::is_nothrow_default_constructible_v<T>)

          requiresstd::default_initializable<T>

         :/*copyable-box*/(std::in_place){}
      (since C++20)
      (until C++23)
      constexpr/*movable-box*/()noexcept(std::is_nothrow_default_constructible_v<T>)

          requiresstd::default_initializable<T>

         :/*movable-box*/(std::in_place){}
      (since C++23)

      The default constructor is provided if and only ifT modelsdefault_initializable.

      A default-constructed wrapper contains a value-initializedT object.

      Assignment operators

      (1)
      constexpr/*copyable-box*/& operator=(const/*copyable-box*/& other);
         noexcept(/* see below */);
      (since C++20)
      (until C++23)
      constexpr/*movable-box*/& operator=(const/*movable-box*/& other);
         noexcept(/* see below */) requiresstd::copy_constructible<T>;
      (since C++23)
      (2)
      constexpr/*copyable-box*/& operator=(/*copyable-box*/&& other)
         noexcept(std::is_nothrow_move_constructible_v<T>);
      (since C++20)
      (until C++23)
      constexpr/*movable-box*/& operator=(/*movable-box*/&& other)
         noexcept(std::is_nothrow_move_constructible_v<T>);
      (since C++23)
      1) Ifstd::copyable<T> is not modeled, the copy assignment operator is equivalently defined as:

      constexpr/*copyable-box*/& operator=(const/*copyable-box*/& other)
         noexcept(std::is_nothrow_copy_constructible_v<T>)
      {
         if(this!=std::addressof(other))
             if(other)
                  emplace(*other);
             else
                  reset();

         return*this;
      }

      (until C++23)

      constexpr/*movable-box*/& operator=(const/*movable-box*/& other)
         noexcept(std::is_nothrow_copy_constructible_v<T>)
          requiresstd::copy_constructible<T>
      {
         if(this!=std::addressof(other))
             if(other)
                  emplace(*other);
             else
                  reset();

         return*this;
      }

      (since C++23)
      Otherwise, it is identical tothe copy assignment operator ofstd::optional.
      2) Ifstd::movable<T> is not modeled, the move assignment operator is equivalently defined as:

      constexpr/*copyable-box*/& operator=(/*copyable-box*/&& other)
         noexcept(std::is_nothrow_move_constructible_v<T>)
      {
         if(this!=std::addressof(other))
             if(other)
                  emplace(std::move(*other));
             else
                  reset();

         return*this;
      }

      (until C++23)

      constexpr/*movable-box*/& operator=(/*movable-box*/&& other)
         noexcept(std::is_nothrow_move_constructible_v<T>)
      {
         if(this!=std::addressof(other))
             if(other)
                  emplace(std::move(*other));
             else
                  reset();

         return*this;
      }

      (since C++23)
      Otherwise, it is identical tothe move assignment operator ofstd::optional.

      Members identical tostd::optional

      Member functions

      constructs theoptional object
      (public member function ofstd::optional<T>)[edit]
      destroys the contained value, if there is one
      (public member function ofstd::optional<T>)[edit]
      assigns contents
      (public member function ofstd::optional<T>)[edit]
      Observers
      accesses the contained value
      (public member function ofstd::optional<T>)[edit]
      checks whether the object contains a value
      (public member function ofstd::optional<T>)[edit]
      Modifiers
      destroys any contained value
      (public member function ofstd::optional<T>)[edit]
      constructs the contained value in-place
      (public member function ofstd::optional<T>)[edit]

      [edit]Notes

      Acopyable-box(until C++23)movable-box(since C++23) does not contain a value only if

      • T does not modelmovable orcopyable, and an exception is thrown on move assignment or copy assignment respectively, or
      • it is initialized/assigned from another valueless wrapper.

      BeforeP2325R3, the wrapper was calledsemiregular-box in the standard and always satisfiedsemiregular, as the default constructor was always provided (which might construct a valueless wrapper).

      Feature-test macroValueStdFeature
      __cpp_lib_ranges201911L(C++20)Ranges library andconstrained algorithms
      202106L(C++20)
      (DR)
      Non-default-initializableviews
      202207L(C++23)Relaxingrange adaptors to allow for move-only types

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      P2325R3C++20ifT is notdefault_initializable, the default constructor
      constructs a wrapper which does not contain a value
      the wrapper is also
      notdefault_initializable
      LWG 3572C++20conditionally different assignment operators were not constexprmade constexpr

      [edit]See also

      aview that contains a single element of a specified value
      (class template)(customization point object)[edit]
      aview consisting of a generated sequence by repeatedly producing the same value
      (class template)(customization point object)[edit]
      aview that consists of the elements of arange that satisfies a predicate
      (class template)(range adaptor object)[edit]
      aview of a sequence that applies a transformation function to each element
      (class template)(range adaptor object)[edit]
      aview consisting of the initial elements of anotherview, until the first element on which a predicate returnsfalse
      (class template)(range adaptor object)[edit]
      aview consisting of the elements of anotherview, skipping the initial subsequence of elements until the first element where the predicate returnsfalse
      (class template)(range adaptor object)[edit]
      aview consisting of results of application of a transformation function to corresponding elements of the adapted views
      (class template)(customization point object)[edit]
      aview consisting of results of application of a transformation function to adjacent elements of the adapted view
      (class template)(range adaptor object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/copyable_wrapper&oldid=174149"

      [8]ページ先頭

      ©2009-2025 Movatter.jp