File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ template <int N> struct check {};
5
+
6
+ // constexpr 함수 - C++11
7
+ constexpr int add (int a, int b)
8
+ {
9
+ return a + b;
10
+ }
11
+
12
+ int main ()
13
+ {
14
+ int n = add (1 , 2 );
15
+
16
+ check< add (1 , 2 ) > c; // ok..
17
+
18
+
19
+ int n1 = 1 , n2 = 2 ;
20
+
21
+ int c = add (n1, n2); // ok
22
+
23
+ // check< add(n1, n2) > c; // error
24
+ }
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * HOME : ecourse.co.kr
3
+ * EMAIL : smkang @ codenuri.co.kr
4
+ * COURSENAME : C++ Template Programming
5
+ * MODULE : factorial.cpp
6
+ * Copyright (C) 2017 CODENURI Inc. All rights reserved.
7
+ */
8
+
9
+ #include < iostream>
10
+ using namespace std ;
11
+
12
+ // template meta programming
13
+ template <int N> struct factorial
14
+ {
15
+ // int value = 10;
16
+ // enum { value = N * factorial<N-1>::value };
17
+ static constexpr int value = N * factorial<N - 1 >::value;
18
+ };
19
+ // 재귀의 종료를 위해 특수화 문법 사용
20
+ template <> struct factorial <1 >
21
+ {
22
+ // enum { value = 1 };
23
+ static constexpr int value = 1 ;
24
+ };
25
+
26
+ int main ()
27
+ {
28
+ int n = factorial<5 >::value; // 5 * 4 * 3 * 2 * 1 => 120
29
+ // 5 * f<4>::v
30
+ // 4 * f<3>::v
31
+ // 3 * f<2>::v
32
+ // 2 * f<1>::v
33
+ // 1
34
+
35
+ cout << n << endl;
36
+ }
37
+
You can’t perform that action at this time.
0 commit comments