Search code examples
c++c++11boostboost-accumulators

How can I use boost accumulator quantile_probability inside a class member initialization?


Boost Accumulator has an unfortunate quirk in which the api interface behaves differently when used inside of a class.

I am trying to use Boost Accumulator quantile_probability inside of a class but I can't figure out how to make it work.

This problem is similar to this issue: Can boost accumulators be used as class members

  #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);  // Doesn't work. ERROR: ‘ba::quantile_probability’ is not a type
  };

  accumulator_t acc(ba::quantile_probability = 0.90);   // I just fine!

Solution

  • The open bracket in accumulator_t myAcc( gets parsed as a member function, in which case this is defining a function taking a variable of type ba::qunatile_probability. But that isn't a type, so it fails.

    You need to write your initializer with = or {, or write it in a constructor's initializer list

    struct Foo {
        // One of these
        accumulator_t myAcc = accumulator_t(ba::quantile_probability = 0.90);
    
        accumulator_t myAcc{ba::quantile_probability = 0.90};
        
        accumulator_t myAcc;
        Foo() : myAcc(ba::quantile_probability = 0.90) {}
    };