Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:13:43+00:00 2026-06-17T22:13:43+00:00

So, assume I have a template structure-function fib<i>::value . I want to get nth

  • 0

So, assume I have a template structure-function fib<i>::value. I want to get nth fibonacci number in runtime. For this i create array fibs[] = { fib<0>::value, ... , fib<maxN>::value }. Unfortunatelly, for some functions maxN can be very large and I can’t fill it with hands only. So I writed some preprocessor directives to make task easier.

#define fib(x) (fib<(x)>::value)
#define fibLine_level_0(x) fib(5*(x) + 0), fib(5*(x) + 1), fib(5*(x) + 2), fib(5*(x) + 3), fib(5*(x) + 4)
#define fibLine_level_1(x) fibLine_level_0(2*(x) + 0), fibLine_level_0(2*(x) + 1)
#define fibLine_level_2(x) fibLine_level_1(2*(x) + 0), fibLine_level_1(2*(x) + 1)
#define fibLine_level_3(x) fibLine_level_2(2*(x) + 0), fibLine_level_2(2*(x) + 1)

#define cAarrSize(x) (sizeof(x) / sizeof(x[0]))

And I use it so:

int fibs[] = { fibLine_level_3(0) };

for (int i = 0; i < cAarrSize(fibs); i++)
    cout << "fib(" << i << ") = " << fibs[i] << endl;

The code that you may need:

template<int i>
struct fibPair{
    static const int fst = fibPair<i-1>::snd;
    static const int snd = fibPair<i-1>::fst + fibPair<i-1>::snd;
};

template<>
struct fibPair<0> {
    static const int fst = 0;
    static const int snd = 1;
};

template<int i>
struct fib {
    static const int value = fibPair<i>::fst;
};

But this code is really ugly. What to do to make it more beautiful?

Constraints: this code must be used in sport programming. That means – no third-party libraries and sometimes no C++11 (but it can be)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:13:44+00:00Added an answer on June 17, 2026 at 10:13 pm

    Fib structure can be rewritten as follows:

    template <size_t i>
    struct fib
    {
        static const size_t value = fib<i - 1>::value + fib<i - 2>::value;
    };
    
    template <>
    struct fib<0>
    {
        static const size_t value = 0;
    };
    
    template <>
    struct fib<1>
    {
        static const size_t value = 1;
    };
    

    Compile-time array of the Fibonacci numbers can be calculated using C++11.

    Edit 1 (changed the type of fib values).

    Edit 2:

    Compile-time generation of Fibonacci numbers array (based on this answer).

    template<unsigned... args> struct ArrayHolder
    {
        static const unsigned data[sizeof...(args)];
    };
    
    template<unsigned... args>
    const unsigned ArrayHolder<args...>::data[sizeof...(args)] = { args... };
    
    template<size_t N, template<size_t> class F, unsigned... args>
    struct generate_array_impl
    {
        typedef typename generate_array_impl<N-1, F, F<N>::value, args...>::result result;
    };
    
    template<template<size_t> class F, unsigned... args>
    struct generate_array_impl<0, F, args...>
    {
        typedef ArrayHolder<F<0>::value, args...> result;
    };
    
    template<size_t N, template<size_t> class F>
    struct generate_array
    {
        typedef typename generate_array_impl<N-1, F>::result result;
    };
    
    int main()
    {
        const size_t count = 10;
        typedef generate_array<count, fib>::result fibs;
    
        for(size_t i = 0; i < count; ++i)
            std::cout << fibs::data[i] << std::endl;
    }
    

    All you need is to provide generate_array with the generation «function» (our fib struct).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have a function like this: static const boost::int32_t SOME_CONST_VALUE = 1073741823; template<typename
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Let's assume we have a template function foo: template<class T> void foo(T arg) {
Assume I have an enumerable object enum and now I want to get the
I have a function template, where I want to do perfect forwarding into a
Assume I have the following exemplary function: template <typename Fn> auto call(Fn fn) ->
Assume you have a function read_key and normally it does some stuff. You someone
Assume I have some templated class template <typename T> struct Dummy { // ...
Assume that I have a boost::function of with an arbitrary signature called type CallbackType
If I have a function template with typename T , where the compiler can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.