vendredi 5 juin 2015

Initializing a static class member array with a non-trivial expression in C++11

I would like to benchmark the performance of using a const cache for some static function inside a cache. So I have something like that:

class Foo {

    static double cost(int factor) { <moderately complex function> };

    // Other stuff using the cost() function

};

And I would like to benchmark against an alternative version like this one:

class Foo {
    private:
        static double _cost(int factor) { <same function as before> };
        static const double cost_cache[MAX_FACTOR] = ???;
    public:
        static double cost(int factor) { return cost_cache[factor]; };
    // Other stuff
}

With a way to initialize my cost_cache array in a way equivalent to

for (int idx = 0; i < MAX_FACTOR; ++i)
    cost_cache[idx] = _cost(idx);

In a high-level functional language I would use a map primitive. How do I properly initialize that in C++11 (or C++14 ?) I saw other posts addressing similar questions, like Initializing private member static const array, but its solution is inapplicable in my case, I can't put the 10k values verbatim in source.

I'm using clang++

Aucun commentaire:

Enregistrer un commentaire