First of all: you cannot completely initialize at compile time with C ++ 03 (yes, by design!) - the only way to do this is to use C ++ templates, but you cannot pass a double parameter to the template.
Everything is getting better w / C ++ 11 - you can use constexpr , but only if your compute_legendre_coeffs() is trivial enough.
Or there is one trick that I use when I need to take some action upon the declaration of a class - for example, register somewhere somewhere ... in order to provide serialization capabilities through some library or smth like this.
You can use static idiom constructors to initialize these arrays ... For simialr reasons, I use the following code:
template < typename Derived , typename Target = Derived > class static_xtors { // This class will actually call your static init methods... struct helper { helper() { Target::static_ctor(); } ~helper() { Target::static_dtor(); } }; // ... because your derived class would inherit this member from static_xtor base static helper s_helper; // The rest is needed to force compiler to instantiate everything required stuff // w/o eliminate as unused... template <void(*)()> struct helper2 {}; static void use_helper() { (void)s_helper; } helper2<&static_xtors::use_helper> s_helper2; virtual void use_helper2() { (void)s_helper2; } public: /// this is not required for your case... only if later you'll have /// a hierarchy w/ virtuals virtual ~static_xtors() {} }; template < typename Derived , typename Target > typename static_xtors<Derived, Target>::helper static_xtors<Derived, Target>::s_helper;
then you need to inherit the static_xtors class and implement two static methods: void static_ctor() - which initializes your arrays and empty (in your case) void static_dtor() ... Ie smth like this:
template <size_t n> struct legendre : public static_xtors<legendre<n>> { static const size_t size = n; static double x[n]; static double w[n]; static void static_ctor() { compute_legendre_coeffs(n, x, w); } static void static_dtor() {
As you can see, x and w no longer constants :( - you can try to make them const hidden again to private and add static getters that will be used by callers ... In addition, your internal arrays will be initialized at runtime, but before the main function (and only once) ...
or play w / constexpr ... but it looks like you will need to redesign the initialization function (one way or another) because, using the initialization lists, it should look like this:
template <size_t n> static double legendre<n>::x[n] = { calc_coeff_x<0>(), calc_coeff_x<1>(), calc_coeff_x<2>(), ... }
... and, of course, you cannot do this without specialization (and using advanced macros). But probably, variable templates can help ... you need to know more details about your function and time to think :))