Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::views::single,std::ranges::single_view

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
      Defined in header<ranges>
      (1)
      template<std::copy_constructible T>

          requiresstd::is_object_v<T>
      class single_view

         :publicranges::view_interface<single_view<T>>
      (since C++20)
      (until C++23)
      template<std::move_constructible T>

          requiresstd::is_object_v<T>
      class single_view

         :publicranges::view_interface<single_view<T>>
      (since C++23)
      namespace views{

         inlineconstexpr/* unspecified */ single=/* unspecified */;

      }
      (2)(since C++20)
      Call signature
      template<class T>

          requires/* see below */

      constexpr/* see below */ single( T&& t);
      (since C++20)
      1) Produces aview that contains exactly one element of a specified value.
      2) The expressionviews::single(e) isexpression-equivalent tosingle_view<std::decay_t<decltype((e))>>(e) for any suitable subexpressione.

      The lifetime of the element is bound to the parentsingle_view. Copyingsingle_view makes a copy of the element.

      Contents

      Customization point objects

      The nameviews::single denotes acustomization point object, which is a constfunction object of aliteralsemiregular class type. SeeCustomizationPointObject for details.

      [edit]Data members

      Member Definition
      copyable-box <T>value_(until C++23) the single element of the view
      (exposition-only member object*)
      movable-box <T>value_(since C++23) the single element of the view
      (exposition-only member object*)

      [edit]Member functions

      constructs asingle_view
      (public member function)
      returns a pointer to the element
      (public member function)
      returns a pointer past the element
      (public member function)
      [static]
      returnsfalse
      (public static member function)
      [static]
      returns1
      (public static member function)
      returns a pointer to the element
      (public member function)
      Inherited fromstd::ranges::view_interface
      (C++23)
      returns a constant iterator to the beginning of the range
      (public member function ofstd::ranges::view_interface<D>)[edit]
      (C++23)
      returns a sentinel for the constant iterator of the range
      (public member function ofstd::ranges::view_interface<D>)[edit]
      returns whether the derived view is not empty, provided only ifranges::empty is applicable to it
      (public member function ofstd::ranges::view_interface<D>)[edit]
      returns the first element in the derived view, provided if it satisfiesforward_range
      (public member function ofstd::ranges::view_interface<D>)[edit]
      returns the last element in the derived view, provided only if it satisfiesbidirectional_range andcommon_range
      (public member function ofstd::ranges::view_interface<D>)[edit]
      returns thenth element in the derived view, provided only if it satisfiesrandom_access_range
      (public member function ofstd::ranges::view_interface<D>)[edit]

      std::ranges::single_view::single_view

      single_view() requiresstd::default_initializable<T>=default;
      (1)(since C++20)
      (2)
      constexprexplicit single_view(const T& t);
      (since C++20)
      (until C++23)
      constexprexplicit single_view(const T& t)
          requiresstd::copy_constructible<T>;
      (since C++23)
      constexprexplicit single_view( T&& t);
      (3)(since C++20)
      template<class...Args>

          requiresstd::constructible_from<T, Args...>

      constexprexplicit single_view(std::in_place_t, Args&&...args);
      (4)(since C++20)

      Constructs asingle_view.

      1) Default initializesvalue_, which value-initializes its contained value.
      2) Initializesvalue_ witht.
      3) Initializesvalue_ withstd::move(t).
      4) Initializesvalue_ as if byvalue_{std::in_place,std::forward<Args>(args)...}.

      std::ranges::single_view::begin

      constexpr T* begin()noexcept;
      constexprconst T* begin()constnoexcept;
      (since C++20)

      Equivalent toreturn data();.

      std::ranges::single_view::end

      constexpr T* end()noexcept;
      constexprconst T* end()constnoexcept;
      (since C++20)

      Equivalent toreturn data()+1;.

      std::ranges::single_view::empty

      staticconstexprbool empty()noexcept;
      (since C++20)

      Equivalent toreturnfalse;.

      std::ranges::single_view::size

      staticconstexprstd::size_t size()noexcept;
      (since C++20)

      Equivalent toreturn1;.

      Makessingle_view model/*tiny-range*/ as required bysplit_view.

      std::ranges::single_view::data

      constexpr T* data()noexcept;
      constexprconst T* data()constnoexcept;
      (since C++20)

      Returns a pointer to the contained value ofvalue_. The behavior is undefined ifvalue_ does not contains a value.

      [edit]Deduction guides

      template<class T>
      single_view( T)-> single_view<T>;
      (since C++20)

      [edit]Notes

      For asingle_view, the inheritedempty member function always returnsfalse, and the inheritedoperatorbool conversion function always returnstrue.

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <ranges>#include <string>#include <tuple> int main(){constexpr std::ranges::single_view sv1{3.1415};// uses (const T&) constructor    static_assert(sv1);    static_assert(not sv1.empty()); std::cout<<"1) *sv1.data(): "<<*sv1.data()<<'\n'<<"2) *sv1.begin(): "<<*sv1.begin()<<'\n'<<"3)  sv1.size(): "<< sv1.size()<<'\n'<<"4)  distance: "<<std::distance(sv1.begin(), sv1.end())<<'\n'; std::string str{"C++20"};std::cout<<"5)  str = "<<std::quoted(str)<<'\n';    std::ranges::single_view sv2{std::move(str)};// uses (T&&) constructorstd::cout<<"6) *sv2.data(): "<<std::quoted(*sv2.data())<<'\n'<<"7)  str = "<<std::quoted(str)<<'\n';     std::ranges::single_view<std::tuple<int,double,std::string>>        sv3{std::in_place,42,3.14,"😄"};// uses (std::in_place_t, Args&&... args) std::cout<<"8)  sv3 holds a tuple: { "<< std::get<0>(sv3[0])<<", "<< std::get<1>(sv3[0])<<", "<< std::get<2>(sv3[0])<<" }\n";}

      Output:

      1) *sv1.data(): 3.14152) *sv1.begin(): 3.14153)  sv1.size(): 14)  distance: 15)  str = "C++20"6) *sv2.data(): "C++20"7)  str = ""8)  sv3 holds a tuple: { 42, 3.14, 😄 }

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3428C++20single_view was convertible fromstd::in_place_tthe constructor is made explicit
      LWG 4035C++20single_view did not provide the member functionempty()providesempty()
      P2367R0C++20deduction guides forsingle_view failed to decay the argument;
      views::single copied but not wrapped asingle_view
      a decaying guide provided;
      made always wrapping

      [edit]See also

      (C++17)
      a wrapper that may or may not hold an object
      (class template)[edit]
      an emptyview with no elements
      (class template)(variable template)[edit]
      aview over the subranges obtained from splitting anotherview using a delimiter
      (class template)(range adaptor object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/single_view&oldid=176146"

      [8]ページ先頭

      ©2009-2025 Movatter.jp