-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathstring_view.cxx
More file actions
32 lines (25 loc) · 855 Bytes
/
string_view.cxx
File metadata and controls
32 lines (25 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#feature on safety
#include <std2.h>
using namespace std2;
int main() safe {
// Populate the vector with views with a nice mixture of lifetimes.
vector<string_view> views { };
// string_view with /static lifetime.
mut views.push_back("From a string literal");
// string_view with outer scope lifetime.
string s1("From string object 1");
mut views.push_back(s1);
{
// string_view with inner scope lifetime.
string s2("From string object 2");
mut views.push_back(s2);
// s2 goes out of scope. views now holds dangling pointers into
// out-of-scope data.
}
// Print the strings. s2 already fell out of scope, so this should
// be a borrowck violation. `views` now contains objects that hold
// dangling pointers.
println("Printing from the outer scope:");
for(string_view sv : views)
println(sv);
}