편집 요약 없음 |
(→예제) |
||
4번째 줄: | 4번째 줄: | ||
== 예제 == | == 예제 == | ||
<source lang="cpp"> | <source lang="cpp"> | ||
int Fibo(int n) | int Fibo(int n)// 재귀 함수 | ||
{ | { | ||
if(n <= 1) return 1; | if(n <= 1) return 1; | ||
return Fibo(n-1) + Fibo(n-2); | return Fibo(n-1) + Fibo(n-2); | ||
} | |||
template <int N> | |||
struct Fibo; | |||
template <> | |||
struct Fibo<0> | |||
{ | |||
enum{ | |||
value = 1 | |||
} | |||
} | |||
template <> | |||
struct Fibo<1> | |||
{ | |||
enum{ | |||
value = 1 | |||
} | |||
} | } | ||
</source> | </source> |
2015년 4월 20일 (월) 12:28 판
C++에서, 템플릿 메타 프로그래밍은 템플릿을 이용하여 컴파일 타임에 코드를 생성하는 것 이다. 보통함수형 프로그래밍 방식으로 개발하며, 일반화 프로그래밍이 가능하다는 장점이 있다.
예제
int Fibo(int n)// 재귀 함수
{
if(n <= 1) return 1;
return Fibo(n-1) + Fibo(n-2);
}
template <int N>
struct Fibo;
template <>
struct Fibo<0>
{
enum{
value = 1
}
}
template <>
struct Fibo<1>
{
enum{
value = 1
}
}