Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::view_interface<D>::operator bool

      From cppreference.com
      <cpp‎ |ranges‎ |view interface
       
       
      Ranges library
      Range adaptors
       
       
      constexprexplicit operatorbool() requires/* see below */;
      (1)(since C++20)
      constexprexplicit operatorbool()const requires/* see below */;
      (2)(since C++20)

      The default implementation ofoperatorbool member function checks whether the view is non-empty. It makes the derived typecontextually convertible tobool.

      1) Letderived bestatic_cast<D&>(*this). The expression in the requires-clause is equal torequires{ranges::empty(derived);}, and the function body is equivalent toreturn!ranges::empty(derived);.
      2) Same as(1), except thatderived isstatic_cast<const D&>(*this).

      Contents

      [edit]Return value

      false if the value of the derived type is empty (determined bystd::ranges::empty),true otherwise.

      [edit]Notes

      In C++20, no type derived fromstd::ranges::view_interface in the standard library provides their ownoperatorbool. Almost all of these types use the default implementation.

      A notable exception isstd::ranges::basic_istream_view. For its iterator type never satisfiesforward_iterator, the view cannot use the inheritedoperatorbool.

      [edit]Example

      Run this code
      #include <array>#include <iostream>#include <ranges> int main(){conststd::array ints{0,1,2,3,4};auto odds= ints| std::views::filter([](int i){return0!= i%2;});auto negs= ints| std::views::filter([](int i){return i<0;});std::cout<<std::boolalpha<<"Has odd numbers: "<<(!!odds)<<' '<<'\n'<<"Has negative numbers: "<<(!!negs)<<' '<<'\n';}

      Output:

      Has odd numbers: trueHas negative numbers: false

      [edit]See also

      checks whether a range is empty
      (customization point object)[edit]
      returns whether the derived view is empty, provided only if it satisfiessized_range orforward_range
      (public member function)[edit]
      (C++17)
      checks whether the container is empty
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/view_interface/operator_bool&oldid=182207"

      [8]ページ先頭

      ©2009-2025 Movatter.jp