summaryrefslogtreecommitdiffhomepage
path: root/CompilerDriver/cc2/regression-tests/mixed-initialization-safety-3.cpp2
blob: 290e50867ed5e2558332a01d3f0f521828d03ea2 (plain)
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
#include <random>
#include <string>
#include <vector>

main: () -> int = {
    x: std::string;         // note: uninitialized!

    if flip_a_coin() {
        x = "xyzzy";
    } else {
        fill( out x, "plugh", 40 ); // note: constructs x!
    }
    print_decorated(x);
}

fill: (
    out x: std::string,
    value: std::string,
    count: int
    )
    pre( value.ssize() >= count, "fill: value must contain at least count elements" )
= {
    x = value.substr(0, count);
}

print_decorated: (x:_) = std::cout << ">> [" << x << "]\n";

// for test determinism, force "xyzzy" branch
// the standard mandates that std::mt19937()() == 3499211612
flip_a_coin: () -> bool = std::mt19937()() % 2 == 0;