Advertisement
Radfler

::fibonacci_number

May 25th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <type_traits>
  2.  
  3. namespace details {
  4.  
  5.   template<std::size_t Value>
  6.   using constant = std::integral_constant<std::size_t, Value>;
  7.  
  8.   template<std::size_t>
  9.   struct fibonacci_number;
  10.  
  11.   template<>
  12.   struct fibonacci_number<0> : constant<0> { };
  13.  
  14.   template<>
  15.   struct fibonacci_number<1> : constant<1> { };
  16.  
  17.   template<>
  18.   struct fibonacci_number<2> : constant<1> { };
  19.  
  20.   template<std::size_t N>
  21.   struct fibonacci_number {
  22.     using _1 = typename fibonacci_number<N - 1>::type;
  23.     using _2 = typename fibonacci_number<N - 2>::type;
  24.    
  25.     using type = constant<_1{} + _2{}>;
  26.   };
  27.  
  28. }
  29.  
  30. template<std::size_t N>
  31. struct fibonacci_number : details::fibonacci_number<N>::type { };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement