forked from hsutter/cppfront
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmixed-initialization-safety-3.cpp
More file actions
72 lines (51 loc) · 2.13 KB
/
mixed-initialization-safety-3.cpp
File metadata and controls
72 lines (51 loc) · 2.13 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <random>
#include <string>
#include <vector>
//=== Cpp2 type declarations ====================================================
#include "cpp2util.h"
#line 1 "mixed-initialization-safety-3.cpp2"
//=== Cpp2 type definitions and function declarations ===========================
#line 1 "mixed-initialization-safety-3.cpp2"
#line 5 "mixed-initialization-safety-3.cpp2"
[[nodiscard]] auto main() -> int;
#line 16 "mixed-initialization-safety-3.cpp2"
auto fill(
cpp2::impl::out<std::string> x,
cpp2::impl::in<std::string> value,
cpp2::impl::in<int> count
) -> void;
#line 26 "mixed-initialization-safety-3.cpp2"
auto print_decorated(auto const& x) -> void;
#line 30 "mixed-initialization-safety-3.cpp2"
// for test determinism, force "xyzzy" branch
// the standard mandates that std::mt19937()() == 3499211612
[[nodiscard]] auto flip_a_coin() -> bool;
//=== Cpp2 function definitions =================================================
#line 1 "mixed-initialization-safety-3.cpp2"
#line 5 "mixed-initialization-safety-3.cpp2"
[[nodiscard]] auto main() -> int{
cpp2::impl::deferred_init<std::string> x; // note: uninitialized!
if (flip_a_coin()) {
x.construct("xyzzy");
}else {
fill(cpp2::impl::out(&x), "plugh", 40);// note: constructs x!
}
print_decorated(cpp2::move(x.value()));
}
#line 16 "mixed-initialization-safety-3.cpp2"
auto fill(
cpp2::impl::out<std::string> x,
cpp2::impl::in<std::string> value,
cpp2::impl::in<int> count
) -> void
{
if (cpp2::cpp2_default.is_active() && !(cpp2::impl::cmp_greater_eq(CPP2_UFCS(ssize)(value),count)) ) { cpp2::cpp2_default.report_violation(CPP2_CONTRACT_MSG("fill: value must contain at least count elements")); }
#line 23 "mixed-initialization-safety-3.cpp2"
x.construct(CPP2_UFCS(substr)(value, 0, count));
}
#line 26 "mixed-initialization-safety-3.cpp2"
auto print_decorated(auto const& x) -> void{
std::cout << ">> [" << x << "]\n";
}
#line 32 "mixed-initialization-safety-3.cpp2"
[[nodiscard]] auto flip_a_coin() -> bool { return std::mt19937()() % CPP2_ASSERT_NOT_ZERO_LITERAL(CPP2_TYPEOF(std::mt19937()()),2) == 0; }