The template element takes the time consumed by the runtime, which is optimized during compilation
Recursion is extremely time consuming
1#include <iostream>2 3 //The template element takes the time consumed by the runtime, which is optimized during compilation4 //recursion is extremely time consuming5 6Template <intN>7 structData8 {9 enum{res = data<n-1>::res + Data<n-2>:: res};Ten }; One ATemplate <> - structdata<1> - { the enum{res =1 }; - }; - -Template <> + structdata<2> - { + enum{res =1 }; A }; at - intGetDataintN//recursive function,Fepolaci Series - { - if(n = =1|| n = =2) - { - return 1; in } - Else to { + returnGetData (N-1) + GetData (N-2); - } the } * $ voidMain ()Panax Notoginseng { - Const intMyint $); the + //< can not have variables, must be constants > A intnum = data<myint>::res;//template meta-programming, Quick the +Std::cout << Num <<Std::endl; - $Std::cout << GetData ( $) << Std::endl;//recursive function, slow $ -System"Pause"); -}
Template meta-programming