You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在类成员初始化中使用Boost Accumulator的quantile_probability

Boost Accumulator类成员初始化问题解决

Boost Accumulator存在一个不便的特性:在类内部使用时,其API接口表现与全局作用域不同。尝试在类中用quantile_probability初始化accumulator_set成员时编译报错,但全局作用域下相同写法却能正常运行。

报错的代码示例:

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp>
#include <boost/accumulators/statistics/weighted_p_square_quantile.hpp>
#include <boost/accumulators/statistics/parameters/quantile_probability.hpp>

namespace ba = boost::accumulators;

typedef ba::accumulator_set<int64_t, ba::stats<ba::tag::weighted_p_square_quantile>, int64_t> accumulator_t;

struct Foo {
    accumulator_t myAcc(ba::quantile_probability = 0.90);  // 编译错误:‘ba::quantile_probability’不是类型
};

accumulator_t acc(ba::quantile_probability = 0.90);   // 全局作用域下可正常运行!

解决方法

有两种可行的方式来正确初始化类内的accumulator成员:

  1. 通过构造函数初始化列表
struct Foo {
    accumulator_t myAcc;
    Foo() : myAcc(ba::quantile_probability = 0.90) {}
};
  1. 使用C++11及以上的类内统一初始化语法
struct Foo {
    accumulator_t myAcc{ba::quantile_probability = 0.90};
};

问题原因

类内部声明成员时,accumulator_t myAcc(ba::quantile_probability = 0.90);会被编译器误解析为函数声明(返回accumulator_t类型,参数带有默认值),而非成员变量的初始化语句。而全局作用域下不存在这种语法歧义,所以相同写法可以正常编译。

内容的提问来源于stack exchange,提问作者Nathan Doromal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 10:35:37