返回提升类型::累加器::标签::平均值
Return type of boost::accumulator::tag::mean
请考虑以下 Boost.Accumulator 示例,其中 value_type
是整数类型:
typedef boost::accumulators::features <
boost::accumulators::tag::sum
, boost::accumulators::tag::min
, boost::accumulators::tag::max
, boost::accumulators::tag::mean
> Features;
typedef boost::accumulators::accumulator_set <
value_type
, Features
> Accumulator;
但是,从accumulator_set
中提取值对于min
、max
和sum
是有意义的,并且返回类型是明确的。(accu
是一个变量(的类型呢:
boost::accumulator::extract_result < tag::mean > ( accu );
我想类型是double
.类型是如何推断的?
我的兴趣被激起了,所以我在 boost 源代码中遵循了它。
我创建了一个value_type = int的累加器。extract_result的结果确实是双重的。
这是在extract_result<>中通过以下行推断出来的:
typename mpl::apply<AccumulatorSet, Feature>::type::result_type
这反过来又取决于这一行:
typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
(其中样本是整数(
这里部分专门用于在除法时将整数强制为双精度:
// partial specialization that promotes the arguments to double for
// integral division.
template<typename Left, typename Right>
struct fdiv_base<Left, Right, typename enable_if<are_integral<Left, Right> >::type>
: functional::divides<double const, double const>
{};
示例程序:
#include <iostream>
#include <typeinfo>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
typedef boost::accumulators::features <
boost::accumulators::tag::sum
, boost::accumulators::tag::min
, boost::accumulators::tag::max
, boost::accumulators::tag::mean
> Features;
typedef boost::accumulators::accumulator_set <
int
, Features
> Accumulator;
using namespace std;
int main()
{
Accumulator acc;
acc(0);
acc(99);
auto mean = boost::accumulators::extract_result < boost::accumulators::tag::mean > (acc);
cout << "mean is " << mean << " of type " << typeid(mean).name() << endl;
}
编辑:
检查:
typename mpl::apply<AccumulatorSet, Feature>::type::result_type
AccumulatorSet
是我们的累加器(从论证推导出extract_result
( Feature
tag::mean
tag::mean
出口impl
这是一个impl::mean_impl<mpl::_1, sum>
extract_result
函数调用find_accumulator<Feature>(acc)
,返回对mpl::apply<Feature>::type
(将累加器作为参数的函数对象(的引用
在这个对象上调用 result(( 调用 mean 的 impl(_1(,其中 _1 是 sum(累加器(,mean_impl可以从中提取sample_type,其中它用于 fdiv<> 参数的类型推断。
不幸的是,模板元函数很难写,甚至更难阅读!
相关文章:
- 计算数组c++的平均值
- 计算平均值,不包括上次得分
- 彩色图像的卤化物处理平均值
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 按平均值替换数组中的元素
- 如何返回多个矢量对象的平均值?C++
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 制作具有平均值的随机数生成器
- 如何初始化升压滚动窗口累加器?
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 如何使用对象制作分数列表并获得平均值
- 浮点格式数组的平均值
- 在二叉搜索树C++中计算平均值
- C 复合赋值运算符 ^= 平均值
- 我们如何并行运行算法的 n 个实例并以有效的方式计算结果函数的平均值?
- 我可以计算多线程数的平均值吗?
- 计算另一个图像像素满足条件的像素值的平均值
- 做一个unordered_multimap键的平均值是更好的方法吗?
- 返回提升类型::累加器::标签::平均值