-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstraint.cpp
More file actions
67 lines (54 loc) · 1.49 KB
/
constraint.cpp
File metadata and controls
67 lines (54 loc) · 1.49 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
/**
requires -std=c++11
***/
#include <iostream>
#include <functional>
using namespace std;
template <typename T>
class constrainted{
public:
T value;
T& other;
function<T(T)> constraint;
constrainted(T& _other, function<T(T)> _constraint):
other(_other), constraint(_constraint){
; // do nothing other than initialization
}
operator T(){
value = constraint(other);
return value;
}
T operator =(const T& substituted){
value = substituted;
other = constraint(value);
return value;
}
};
void example1(){
bool a = false;
constrainted<bool> b(a, [](bool x)->bool { return !x; }); // a and b satsfy "a = !b, b = !a"
if(b)
cout << "b is true" << endl;
// Chaning the value of b automatically changes the value of a as well
b = false;
if(a)
cout << "a is true" << endl;
cout << "--------------------------------------" << endl;
}
void example2(){
double alpha;
constrainted<double> beta(alpha, [](double x)->double { return 1-x; }); // alpha and beta satsfy "alpha = 1 - beta, beta = 1 - alpha"
alpha = 0.2;
cout << "alpha: " << alpha << endl; // 0.2
cout << "beta: " << beta << endl; // 0.8
cout << "--------------------------------------" << endl;
// Chaning the value of beta automatically changes the value of alpha as well
beta = 0.3;
cout << "alpha: " << alpha << endl; // 0.7
cout << "beta: " << beta << endl; // 0.3
cout << "--------------------------------------" << endl;
}
main(){
example1();
example2();
}