Last active
June 15, 2018 00:08
-
-
Save j2doll/3615f189dc16d154e086d83220189e1a to your computer and use it in GitHub Desktop.
Template metaprogramming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Template metaprogramming | |
| // | |
| /* 템플릿 메타프로그래밍(template metaprogramming)은 템플릿을 사용하는 프로그래밍 기법으로, | |
| 컴파일러에게 프로그램 코드를 생성하도록 하는 방식이다. 이러한 기법은 컴파일 시점에 많은 것을 | |
| 결정하도록 하여, 실행 시점의 계산을 줄여준다. */ | |
| #include <cstdio> | |
| #include <cstdlib> | |
| template <int N> | |
| struct Factorial { | |
| enum { value = N * Factorial<N - 1>::value }; | |
| }; | |
| template <> | |
| struct Factorial<0> { | |
| enum { value = 1 }; | |
| }; | |
| int main(int argc, char* argv[]) | |
| { | |
| int ret = Factorial<2>::value; | |
| // 0,1,2에 대한 팩토리얼 코드가 모두 만들어진다. | |
| printf("%d\n", ret); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment