名稱空間
變體
操作

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

來自 cppreference.com
 
 
範圍庫 (Ranges library)
範圍介面卡 (Range adaptors)
 
 
constexpr explicit operator bool() requires /* 參見下文 */;
(1) (C++20 起)
constexpr explicit operator bool() const requires /* 參見下文 */;
(2) (C++20 起)

operator bool 成員函式的預設實現檢查檢視是否非空。它使派生型別可以上下文轉換為 bool

1)derivedstatic_cast<D&>(*this)。requires-子句中的表示式等價於 requires { ranges::empty(derived); },函式體等價於 return !ranges::empty(derived);
2)(1),但 derivedstatic_cast<const D&>(*this)

目錄

[編輯] 引數

(無)

[編輯] 返回值

如果派生型別的值為空(由 std::ranges::empty 確定),則為 false,否則為 true

[編輯] 注意

在 C++20 中,標準庫中沒有從 std::ranges::view_interface 派生的型別提供它們自己的 operator bool。幾乎所有這些型別都使用預設實現。

一個值得注意的例外是 std::ranges::basic_istream_view。由於其迭代器型別從不滿足 forward_iterator,該檢視不能使用繼承的 operator bool

[編輯] 示例

#include <array>
#include <iostream>
#include <ranges>
 
int main()
{
    const std::array ints {0, 1, 2, 3, 4};
    auto odds = ints | std::views::filter([](int i) { return 0 != 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';
}

輸出

Has odd numbers: true
Has negative numbers: false

[編輯] 另見

檢查範圍是否為空
(定製點物件)[編輯]
返回派生檢視是否為空,僅當其滿足 sized_rangeforward_range 時才提供
(公有成員函式) [編輯]
(C++17)
檢查容器是否為空
(函式模板) [編輯]