【问题标题】:Initializing a static class member array with a non-trivial expression in C++11在 C++11 中使用非平凡表达式初始化静态类成员数组
【发布时间】:2015-06-05 09:58:17
【问题描述】:

我想对缓存中的某些静态函数使用 const 缓存的性能进行基准测试。所以我有类似的东西:

class Foo {

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

    // Other stuff using the cost() function

};

并且我想与像这样的替代版本进行基准测试:

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
}

通过一种方式来初始化我的 cost_cache 数组,相当于

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

在高级函数式语言中,我会使用地图原语。如何在 C++11(或 C++14?)中正确初始化它在源代码中。

我正在使用clang++

【问题讨论】:

    标签: c++11 clang++


    【解决方案1】:

    先用“{}”初始化数组怎么样, 然后通过从文件加载值的方法覆盖元素?

    【讨论】:

    • 但是 AFAIK 需要从数组中删除 const 修饰符,我不想失去这种类型安全性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 2011-08-04
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多