Skip to content

Commit 8a91d67

Browse files
authored
Add files via upload
1 parent cd3f7bc commit 8a91d67

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

constexpr.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+

factorial.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+

0 commit comments

Comments
 (0)