C++ 许多Boost::histogram对象的动态分配

C++ 许多Boost::histogram对象的动态分配,c++,boost,histogram,C++,Boost,Histogram,我需要动态创建(并在以后删除)许多Boost::histogram对象,每个对象具有不同数量的轴和bin边界,但我认为我无法使用make_histogram factory函数创建一个对象。它不返回指针,因此我无法删除该对象。有人能提供示例代码来动态分配单个直方图吗?我知道,查看代码并查看所有模板和自动返回类型可能会让人感到害怕 您可以做的是让自己成为几个方便的工厂,以获得唯一(或共享)指针: #包括 #包括 #包括 模板 使用(存储和存储、轴和轴、, 轴(&…轴){ 自动a= std::mak

我需要动态创建(并在以后删除)许多Boost::histogram对象,每个对象具有不同数量的轴和bin边界,但我认为我无法使用make_histogram factory函数创建一个对象。它不返回指针,因此我无法删除该对象。有人能提供示例代码来动态分配单个直方图吗?

我知道,查看代码并查看所有模板和
自动
返回类型可能会让人感到害怕

您可以做的是让自己成为几个方便的工厂,以获得唯一(或共享)指针:

#包括
#包括
#包括
模板
使用(存储和存储、轴和轴、,
轴(&…轴){
自动a=
std::make_tuple(std::forward(轴),std::forward(轴)…);
使用U=boost::histogram::detail::remove\U cvref\U t;
使用S=boost::mp11::mp\u if;
return std::make_unique(
标准:移动(a),S(标准:向前(存储));
}
模板
自动生成唯一直方图(轴和轴、轴和…轴){
返回make_unique_histogram_with(boost::histogram::default_storage(),
标准:正向(轴),
标准:正向(轴);
}
int main(){
自动直方图=使直方图唯一(
boost::直方图::轴::常规(6,-1.0,2.0,“x”);
(*直方图)(0.1,boost::直方图::权重(1.0));
}
代码取自boost/histogram/make_histogram.hpp,稍作修改。以类似的方式,您可以重写其余的帮助程序

提醒:您需要一个与C++14兼容的编译器

#include <boost/histogram.hpp>
#include <memory>
#include <tuple>

template <class Storage, class Axis, class... Axes,
          class = boost::histogram::detail::requires_axis<Axis>>
auto make_unique_histogram_with(Storage&& storage, Axis&& axis,
                                Axes&&... axes) {
  auto a =
      std::make_tuple(std::forward<Axis>(axis), std::forward<Axes>(axes)...);
  using U = boost::histogram::detail::remove_cvref_t<Storage>;
  using S = boost::mp11::mp_if<boost::histogram::detail::is_storage<U>, U,
                               boost::histogram::storage_adaptor<U>>;
  return std::make_unique<boost::histogram::histogram<decltype(a), S>>(
      std::move(a), S(std::forward<Storage>(storage)));
}

template <class Axis, class... Axes,
          class = boost::histogram::detail::requires_axis<Axis>>
auto make_unique_histogram(Axis&& axis, Axes&&... axes) {
  return make_unique_histogram_with(boost::histogram::default_storage(),
                                    std::forward<Axis>(axis),
                                    std::forward<Axes>(axes)...);
}

int main() {
  auto histogram = make_unique_histogram(
      boost::histogram::axis::regular<>(6, -1.0, 2.0, "x"));
  (*histogram)(0.1, boost::histogram::weight(1.0));
}