File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed
Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 11# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22
3- set (ALL_EXAMPLES scope_example)
3+ set (ALL_EXAMPLES scope_example unique_resource )
44
55message ("Examples to be built: ${ALL_EXAMPLES} " )
66
Original file line number Diff line number Diff line change 11// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22
3- int main () {}
3+ #include < iostream>
4+ #include < memory>
5+ #include < beman/scope.hpp>
6+
7+ constexpr size_t arr_size = 10 ;
8+
9+ int main () {
10+
11+ {
12+ // Allocate an array
13+ auto resoure_ptr = beman::unique_resource
14+ (
15+ new int [arr_szie], // acquire array resource
16+ // Cleanup function
17+ [](int * ptr) { delete[] ptr;
18+ std::cout << " Array deleted.\n " ; }
19+ );
20+
21+ // Use the array
22+ for (size_t i = 0 ; i < size; ++i) {
23+ resource_ptr.get ()[i] = static_cast <int >(i * 2 );
24+ }
25+ std::cout << " First element: " << resource_ptr.get ()[0 ] << " \n " ;
26+ }
27+
28+ // Resource is automatically released when `resource_ptr` goes out of scope
29+ std::cout << " After scope: \n " ;
30+
31+ }
32+
You can’t perform that action at this time.
0 commit comments