summaryrefslogtreecommitdiffhomepage
path: root/CompilerDriver/cc2/regression-tests/mixed-initialization-safety-2-error.cpp2
blob: 39da2f34dd4fe0f893af07644a176d8fc530c6e7 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include <string>
#include <vector>
#include <mutex>
#include <cstdlib>
#include <ctime>
#include <cstdio>

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

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);
}

//  Print! A one-expression function body...
//
print_decorated: (x:_) = std::cout << ">> [" << x << "]\n";

//  Flip a coin! Exercise <mutex> <cstdlib> <ctime> and 'as'...
//
rand_init_flag: std::once_flag = ();

flip_a_coin: () -> bool = {
    std::call_once(rand_init_flag, srand, time(nullptr) as unsigned );
    return std::rand()%2;
}