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 8636877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:20:12+00:00 2026-06-12T10:20:12+00:00

Let’s assume, I have some sequence, e.g. Fibonacci numbers, defined as a template: template

  • 0

Let’s assume, I have some sequence, e.g. Fibonacci numbers, defined as a template:

template <unsigned int N> struct Fibonacci { unsigned int value = /*...*/; };

What I need is to obtain constexpr array with the first N elements of this sequence. I can do it, using variadic template:

template <unsigned int ... Numbers>
struct FibArray
{
    static constexpr array<unsigned int, sizeof...(Numbers)> value = { Fibonacci<Numbers>::value... };
};
// and then:
const auto fib_array = FibArray<1, 2, 3, 4, 5, 6, 7>::value;

Is it possible, to avoid manual enumeration of indexes, and get the same array with just a number of required values? Something like this:

const array<unsigned, 7> fib_array = GetFirstNFibValues<7>::value;
  • 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-12T10:20:13+00:00Added an answer on June 12, 2026 at 10:20 am

    You can get this behaviour by generating the indices:

    template<unsigned...> struct indices{};
    
    template<unsigned N, unsigned... Indices>
    struct indices_gen : indices_gen<N-1, N-1, Indices...>{};
    
    template<unsigned... Indices>
    struct indices_gen<1, Indices...>{
      using type = indices<1, Indices...>;
    };
    
    #include <array>
    
    template<unsigned N>
    struct fibonacci{
      static constexpr unsigned value = N; // yes, lazyness on my part
    };
    
    template<class IPack>
    struct first_n_fib_impl;
    
    template<unsigned... Is>
    struct first_n_fib_impl<indices<Is...>>
    {
      using arr_type = std::array<unsigned, sizeof...(Is)>;
      static constexpr arr_type value = {{ fibonacci<Is>::value... }};
    };
    
    template<unsigned... Is>
    constexpr std::array<unsigned, sizeof...(Is)> first_n_fib_impl<indices<Is...>>::value;
    
    template<unsigned N>
    struct first_n_fib
      : first_n_fib_impl<typename indices_gen<N>::type>
    {
    };
    

    Live example (to show that [1..7] is indeed generated).

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

Sidebar

Related Questions

Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let say I have these: typedef id Title; typedef struct{ Title title; int pages;
Let's assume that a user votes for some movies in a scale of 1
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let assume we have two activities. A - main activity, that is home launcher
Let's say you have a method that expects a numerical value as an argument.
Let's say I have two text files that I need to extract data out
Let's say I have some data in a CouchDB database. The overall size is
Let me try to explain what I need. I have a server that is
Let's say I have 2 functions: void function1(int *ptr) { printf(%d, *ptr); } and

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.