forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IF.cpp
54 lines (42 loc) · 1006 Bytes
/
IF.cpp
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
//
// Created by light on 19-12-28.
//
#include <iostream>
using namespace std;
// 使用template实现IF条件判断
template<bool cond,
typename Then,
typename Else>
struct IF;
template<typename Then,
typename Else>
struct IF<true, Then, Else> {
typedef Then result;
};
template<typename Then,
typename Else>
struct IF<false, Then, Else> {
typedef Else result;
};
// 判断奇数与偶数
template<int N>
struct isEven {
static const auto RES = IF<N & 1 == 0, true_type, false_type>::result::value;
};
template<int nums1, int nums2>
struct Add_ {
static const int value = nums1 + nums2;
};
template<int nums1, int nums2>
struct Sub_ {
static const int value = nums1 - nums2;
};
// 加减
template<bool cond, int nums1, int nums2>
struct addSub {
static const auto RES = IF<cond, Add_<nums1, nums2>, Sub_<nums1, nums2>>::result::value;
};
int main() {
cout << isEven<10>::RES << endl;
cout << addSub<true, 10, 2>::RES << endl;
}