Skip to content

Instantly share code, notes, and snippets.

@j2doll
Last active June 15, 2018 00:08
Show Gist options
  • Select an option

  • Save j2doll/3615f189dc16d154e086d83220189e1a to your computer and use it in GitHub Desktop.

Select an option

Save j2doll/3615f189dc16d154e086d83220189e1a to your computer and use it in GitHub Desktop.
Template metaprogramming
//
// 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