From cppreference.com
structdangling;(since C++20)dangling is a placeholder type and an empty class type, used together with the template aliases
and
.
When some
that usually return an iterator or a subrange of a
take a particular rvalue range argument that does not model
, dangling will be returned instead to avoid returning potentially dangling results.
Member functions
std::ranges::dangling::dangling
constexprdangling()noexcept=default; (1) template<class...Args>constexprdangling(Args&&...)noexcept{} (2) 1)dangling is trivially default constructible.
2)dangling can be constructed from arguments of arbitrary number and arbitrary non-void type. The construction does not have any side-effect itself.
In other words, after replacing the type (e.g. an iterator type) in a well-formed non-aggregate initialization with dangling, the resulting initialization is also well-formed.
Example
Run this code
#include<algorithm>#include<array>#include<iostream>#include<ranges>#include<type_traits>#include<string_view>intmain(){autoget_array_by_value=[]{returnstd::array{0,1,0,1};};autodangling_iter=std::ranges::max_element(get_array_by_value());static_assert(std::is_same_v<std::ranges::dangling,decltype(dangling_iter)>);// std::cout << *dangling_iter << '\n'; // compilation error: no match for 'operator*'// (operand type is 'std::ranges::dangling')autoget_persistent_array=[]()->conststd::array<int,4>&{staticconstexprstd::arraya{0,1,0,1};returna;};autovalid_iter=std::ranges::max_element(get_persistent_array());static_assert(!std::is_same_v<std::ranges::dangling,decltype(valid_iter)>);std::cout<<*valid_iter<<' ';// 1autoget_string_view=[]{returnstd::string_view{"alpha"};};autovalid_iter2=std::ranges::min_element(get_string_view());// OK: std::basic_string_view models borrowed_rangestatic_assert(!std::is_same_v<std::ranges::dangling,decltype(valid_iter2)>);std::cout<<'\''<<*valid_iter2<<'\''<<'\n';// 'a'}Output:
1 'a' See also