aboutsummaryrefslogtreecommitdiff
path: root/examples/cppx/Value_(CppCon_2017).cpp
blob: 503cffe416f216c10a567d487228c4a7eb073529 (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
46
47
//====================================================================
// Library code: implementing the metaclass (once)

$class basic_value {
    basic_value() = default;
    basic_value(const basic_value& that) = default;
    basic_value(basic_value&& that) = default;
    basic_value& operator=(const basic_value& that) = default;
    basic_value& operator=(basic_value&& that) = default;

    constexpr {
        for... (auto f : $basic_value.variables())
            if (!f.has_access()) f.make_private();
        for... (auto f : $basic_value.functions()) {
            if (!f.has_access()) f.make_public();
            compiler.require(!f.is_protected(), "a value type may not have a protected function");
            compiler.require(!f.is_virtual(),   "a value type may not have a virtual function");
            compiler.require(!f.is_destructor() || f.is_public(), "a value destructor must be public");
        }
    }
};

$class value : basic_value { };


//====================================================================
// User code: using the metaclass to write a type (many times)

value Point {
    int x = 0, y = 0;
    Point(int xx, int yy) : x{xx}, y{yy} { }
};

Point get_some_point() { return {1,1}; }

int main() {

    Point p1(50,100), p2;
    p2 = get_some_point();
    p2.x = 42;

}

// Compiler Explorer note: Click the "triangle ! icon" to see the output:
constexpr {
    compiler.debug($Point);
}