Skip to content

Instantly share code, notes, and snippets.

@b4284
Created March 29, 2016 07:06
Show Gist options
  • Select an option

  • Save b4284/55c53ca021e816188d4b to your computer and use it in GitHub Desktop.

Select an option

Save b4284/55c53ca021e816188d4b to your computer and use it in GitHub Desktop.
#include <iostream>
template<int n>
struct fibonacci
{
static constexpr int value = fibonacci<n-1>::value + fibonacci<n-2>::value;
};
template<>
struct fibonacci<0>
{
static constexpr int value = 0;
};
template<>
struct fibonacci<1>
{
static constexpr int value = 1;
};
int main(void)
{
std::cout << fibonacci<0>::value << std::endl;
std::cout << fibonacci<1>::value << std::endl;
std::cout << fibonacci<2>::value << std::endl;
std::cout << fibonacci<3>::value << std::endl;
std::cout << fibonacci<4>::value << std::endl;
std::cout << fibonacci<5>::value << std::endl;
std::cout << fibonacci<6>::value << std::endl;
std::cout << fibonacci<7>::value << std::endl;
std::cout << fibonacci<8>::value << std::endl;
std::cout << fibonacci<9>::value << std::endl;
std::cout << fibonacci<10>::value << std::endl;
return 0;
}
@whitglint
Copy link
Copy Markdown

#include <iostream>

template<int n>
struct fibonacci
{
    static constexpr int value = fibonacci<n - 1>::value + fibonacci<n - 2>::value;
};
template<>
struct fibonacci<0>
{
    static constexpr int value = 0;
};
template<>
struct fibonacci<1>
{
    static constexpr int value = 1;
};

template<int n>
struct fib_printer
{
    fib_printer() { fib_printer<n - 1>(); std::cout << fibonacci<n>::value << std::endl; }
};
template<>
struct fib_printer<0>
{
    fib_printer() { std::cout << fibonacci<0>::value << std::endl; }
};

int main()
{
    fib_printer<10>();
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment