ranges::dangling::  dangling 
    * 2.1  Example 
    * 2.2  See also 

  
---  
  
###  [ [ edit
](/mwiki/index.php?title=cpp/ranges/dangling&action=edit&section=1 "Edit
section: Member functions") ]  Member functions

##  std::ranges::dangling::  dangling

constexpr  dangling  (  )  noexcept  =  default  ;  |  (1)  |   
---|---|---  
template  < class  ...  Args  >  
constexpr  dangling  (  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.

###  [ [ edit
](/mwiki/index.php?title=cpp/ranges/dangling&action=edit&section=2 "Edit
section: Example") ]  Example

Run this code

    
    
    #include <algorithm>
    #include <array>
    #include <iostream>
    #include <ranges>
    #include <type_traits>
    #include <string_view>
     
    int main()
    {
        auto get_array_by_value = [] { return [std::array](http://en.cppreference.com/w/cpp/container/array){0, 1, 0, 1}; };
        auto dangling_iter = std::[ranges::max_element](http://en.cppreference.com/w/cpp/algorithm/ranges/max_element)(get_array_by_value());
        static_assert([std::is_same_v](http://en.cppreference.com/w/cpp/types/is_same)<std::[ranges::dangling](http://en.cppreference.com/w/cpp/ranges-dangling-placeholder/dangling), decltype(dangling_iter)>);
    //  std::cout << *dangling_iter << '\n'; // compilation error: no match for 'operator*'
                                             // (operand type is 'std::ranges::dangling')
     
        auto get_persistent_array = []() -> const [std::array](http://en.cppreference.com/w/cpp/container/array)<int, 4>& {
            static constexpr [std::array](http://en.cppreference.com/w/cpp/container/array) a{0, 1, 0, 1};
            return a;
        };
        auto valid_iter = std::[ranges::max_element](http://en.cppreference.com/w/cpp/algorithm/ranges/max_element)(get_persistent_array());
        static_assert(![std::is_same_v](http://en.cppreference.com/w/cpp/types/is_same)<std::[ranges::dangling](http://en.cppreference.com/w/cpp/ranges-dangling-placeholder/dangling), decltype(valid_iter)>);
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << *valid_iter << ' '; // 1
     
     
        auto get_string_view = [] { return [std::string_view](http://en.cppreference.com/w/cpp/string/basic_string_view){"alpha"}; };
        auto valid_iter2 = std::[ranges::min_element](http://en.cppreference.com/w/cpp/algorithm/ranges/min_element)(get_string_view());
            // OK: std::basic_string_view models borrowed_range
        static_assert(![std::is_same_v](http://en.cppreference.com/w/cpp/types/is_same)<std::[ranges::dangling](http://en.cppreference.com/w/cpp/ranges-dangling-placeholder/dangling), decltype(valid_iter2)>);
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << '\'' << *valid_iter2 << '\'' << '\n'; // 'a'
    }

Output:

    
    
    1 'a'

###  [ [ edit
](/mwiki/index.php?title=cpp/ranges/dangling&action=edit&section=3 "Edit
section: See also") ]  See also

[ ranges::borrowed_iterator_t  ranges::borrowed_subrange_t  ](/w/cpp/ranges/borrowed_iterator_t "cpp/ranges/borrowed iterator t") (C++20)  |  obtains iterator type or ` subrange ` type of a [ ` borrowed_range ` ](/w/cpp/ranges/borrowed_range "cpp/ranges/borrowed range")   
(alias template)  [ [edit]
](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/ranges/dsc_borrowed_iterator_t&action=edit)  
---|---  
[ ranges::borrowed_range  ](/w/cpp/ranges/borrowed_range "cpp/ranges/borrowed range") (C++20)  |  specifies that a type is a [ ` range ` ](/w/cpp/ranges/range "cpp/ranges/range") and iterators obtained from an expression of i