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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:42:41+00:00 2026-06-10T04:42:41+00:00

Suppose I have some constexpr function f: constexpr int f(int x) { … }

  • 0

Suppose I have some constexpr function f:

constexpr int f(int x) { ... }

And I have some const int N known at compile time:

Either

#define N ...;

or

const int N = ...;

as needed by your answer.

I want to have an int array X:

int X[N] = { f(0), f(1), f(2), ..., f(N-1) }

such that the function is evaluated at compile time, and the entries in X are calculated by the compiler and the results are placed in the static area of my application image exactly as if I had used integer literals in my X initializer list.

Is there some way I can write this? (For example with templates or macros and so on)

Best I have: (Thanks to Flexo)

#include <iostream>
#include <array>
using namespace std;

constexpr int N = 10;
constexpr int f(int x) { return x*2; }

typedef array<int, N> A;

template<int... i> constexpr A fs() { return A{{ f(i)... }}; }

template<int...> struct S;

template<int... i> struct S<0,i...>
{ static constexpr A gs() { return fs<0,i...>(); } };

template<int i, int... j> struct S<i,j...>
{ static constexpr A gs() { return S<i-1,i,j...>::gs(); } };

constexpr auto X = S<N-1>::gs();

int main()
{
        cout << X[3] << endl;
}
  • 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-10T04:42:43+00:00Added an answer on June 10, 2026 at 4:42 am

    There is a pure C++11 (no boost, no macros too) solution to this problem. Using the same trick as this answer we can build a sequence of numbers and unpack them to call f to construct a std::array:

    #include <array>
    #include <algorithm>
    #include <iterator>
    #include <iostream>
    
    template<int ...>
    struct seq { };
    
    template<int N, int ...S>
    struct gens : gens<N-1, N-1, S...> { };
    
    template<int ...S>
    struct gens<0, S...> {
      typedef seq<S...> type;
    };
    
    constexpr int f(int n) {
      return n;
    }
    
    template <int N>
    class array_thinger {
      typedef typename gens<N>::type list;
    
      template <int ...S>
      static constexpr std::array<int,N> make_arr(seq<S...>) {
        return std::array<int,N>{{f(S)...}};
      }
    public:
      static constexpr std::array<int,N> arr = make_arr(list()); 
    };
    
    template <int N>
    constexpr std::array<int,N> array_thinger<N>::arr;
    
    int main() {
      std::copy(begin(array_thinger<10>::arr), end(array_thinger<10>::arr), 
                std::ostream_iterator<int>(std::cout, "\n"));
    }
    

    (Tested with g++ 4.7)

    You could skip std::array entirely with a bit more work, but I think in this instance it’s cleaner and simpler to just use std::array.

    You can also do this recursively:

    #include <array>
    #include <functional>
    #include <algorithm>
    #include <iterator>
    #include <iostream>
    
    constexpr int f(int n) {
      return n;
    }
    
    template <int N, int ...Vals>
    constexpr
    typename std::enable_if<N==sizeof...(Vals),std::array<int, N>>::type
    make() {
      return std::array<int,N>{{Vals...}};
    }
    
    template <int N, int ...Vals>
    constexpr
    typename std::enable_if<N!=sizeof...(Vals), std::array<int,N>>::type 
    make() {
      return make<N, Vals..., f(sizeof...(Vals))>();  
    }
    
    int main() {
      const auto arr = make<10>();
      std::copy(begin(arr), end(arr), std::ostream_iterator<int>(std::cout, "\n"));
    }
    

    Which is arguably simpler.

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

Sidebar

Related Questions

Suppose I have some code like this: class Base { public: virtual int Foo(int)
Suppose I have some div, and I use .animate({opacity:0},400, function(){}); for its children. Is
Suppose I have some Ant task - say javac or junit - if either
Suppose I have some macro #define NAME name , and I want to define
Suppose I have some kind of factory function which creates objects that are largely
Suppose I have some code that would, in theory, compile against any version of
Suppose I have some simple struct like this: public struct WeightedInt { public int
Suppose I have some code like this: public string SomeMethod(int Parameter) { string TheString
Suppose you have some content that is guaranteed to have some value. Your job
Suppose you have some complex JSON like: {A : valA, B : { C

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.