Question: Here is a code snippet illustrating my question:

```
const float D    = 0.1F;
const float A[4] = {sin(0*D), sin(1*D), sin(2*D), sin(3*D)};

```

Imagine that global array `A` is much longer and you don't want to do all of this repetitive typing. Is there a shorter way to initialize array A at compile or initialization time, i.e. without having to write initialization function and call it somewhere in my program?

Answer: 
You could initialize A during dynamic initialization time as follows:

```
const float *init_a(float x_)
{
  static float data[4];
  for(unsigned i=0; i<4; ++i)
    data[i]=sin(i*x_);
  return data;
}

const float D=0.1f;
const float *A=init_a(D);

```
You could either use [boost.preprocessor](http://www.boost.org/doc/libs/1_43_0/libs/preprocessor/doc/index.html) and in particular the `BOOST_PP_ENUM` macro, like the example below:

```
#include <iostream>
#include <cmath>
#include <boost/preprocessor/repetition/enum.hpp>

#define SIZE 4
#define D    0.1
#define ORDER(z, n, text) std::sin(n * D)

double const A[SIZE] = { BOOST_PP_ENUM(SIZE, ORDER, ~) };

int main() {
  for(auto i : A) std::cout << i << std::endl;
}

```

Or, you could use `std::array` instead of raw arrays, and via use of template meta-programming to generate a `std::array` at compile time. like the example below:

```
template<typename T, typename F, int SIZE, int... N> 
constexpr std::array<T, SIZE> 
genarray(F f) { 
  return std::array<T, SIZE>{{ f(N)... }};
}

template<typename T, typename F, int SIZE, int...> struct recursive_gen;

template<typename T, typename F, int SIZE, int... Args> 
struct recursive_gen<T, F, SIZE, 0, Args...> { 
  static constexpr std::array<T, SIZE> generate(F f) {
    return genarray<T, F, SIZE, 0, Args...>(f);
  }
};

template<typename T, typename F, int SIZE, int N, int... Args> 
struct recursive_gen<T, F, SIZE, N, Args...> {
  static constexpr std::array<T, SIZE> generate(F f) {
    return recursive_gen<T, F, SIZE, N - 1, N, Args...>::generate(f); 
  } 
};

template<typename T, int SIZE>
struct array_generator {
  template<typename F>
  static constexpr std::array<T, SIZE> generate(F f) {
    return recursive_gen<T, F, SIZE, SIZE - 1>::generate(f);
  }
};

std::array<double, 4> const A = array_generator<double, 4>::generate([](int i) { return std::sin(0.1 * i);});
std::array<double, 4> const B = array_generator<double, 4>::generate([](int i) { return std::cos(0.1 * i);});

constexpr int fun(int i) { return 2 * i; } 
constexpr std::array<int, 4> const C = array_generator<int, 4>::generate(fun); // generation during compile time

```

[**LIVE DEMO**](http://coliru.stacked-crooked.com/a/e1951206cfbd6dd2)
---------------------------------------------------------------------

Note however, that in order for generation to take place at compile time input function in `array_generator` must be `constexpr`. This is not the case for trigonometric functions (i.e., they are not `constexpr`). Thus initialization of `array`s `A` and `B` will take place at initialization time, whereas generation of `array C` will take place at compile time.