language: C++11 (gcc-4.7.2)
date: 459 days 12 hours ago
link:
visibility: public
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
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
 
template<class T, class Op>
struct bound_op : Op{
  T const& lhs;
  mutable bool val;
  
  bound_op(T const& lhs, bool val)
    : lhs(lhs), val(val) {}
  
  template<class U, class Op2>
  bound_op const& operator()(U const& rhs, Op2 op) const{
    val = op.apply(val, this->apply(lhs, rhs));
    return *this;
  }
  
  explicit operator bool() const{
    return val;
  }
};
 
template<class T>
struct binding_op{
  T const& lhs;
};
 
template<class T>
binding_op<T> chain(T const& lhs){
  return { lhs };
}
 
struct equal_to{
  template<class T, class U>
  bool apply(T const& lhs, U const& rhs) const{
    return lhs == rhs;
  }
};
 
struct not_equal_to{
  template<class T, class U>
  bool apply(T const& lhs, U const& rhs) const{
    return lhs != rhs;
  }
};
 
struct logical_or{
  template<class T, class U>
  bool apply(T const& lhs, U const& rhs) const{
    return lhs || rhs;
  }
};struct logical_and{
  template<class T, class U>
  bool apply(T const& lhs, U const& rhs) const{
    return lhs && rhs;
  }
};
 
template<class T, class U>
bound_op<T, not_equal_to> operator!=(binding_op<T> const& bop, U const& rhs){
  return { bop.lhs, bop.lhs != rhs };
}
 
template<class T, class U>
bound_op<T, equal_to> operator==(binding_op<T> const& bop, U const& rhs){
  return { bop.lhs, bop.lhs == rhs };
}
 
template<class T, class Op, class U>
bound_op<T, Op> operator||(bound_op<T, Op> const& lhs, U const& rhs){
  return lhs(rhs, logical_or());
}
 
template<class T, class Op, class U>
bound_op<T, Op> operator&&(bound_op<T, Op> const& lhs, U const& rhs){
  return lhs(rhs, logical_and());
}
 
int main(){
  int n = 5;
  if(chain(n) != 1 && 2 && 3 && 4 && 0 && 5)
   std::cout << "Yes - not any of 0..5!\n";
  if(chain(n) == 1 || 2 || 3 || 4 || 0 || 5)
   std::cout << "Yes - one of 0..5!\n";
}