在运行时提升mpl下限

boost mpl lower_bound at runtime

本文关键字:mpl 下限 运行时      更新时间:2023-10-16

在下面的代码中,我有一个为特定大小构建的类SizedFoo。类GeneralFoo保存不同大小的SizedFoos的数组,然后函数Bar将找到为大于传入参数的最小大小而初始化的SizedFoo。我知道boost mpl有下限函数,但它只能与静态已知的值一起使用,有什么方法可以在运行时使用等效函数吗?

class SizedFoo {
  ...
public:
  SizedFoo(size_t size);
  void Bar();
}
template<size_t... sizes_in>
class GeneralFoo {
  typedef vector_c<size_t, sizes_in...>::type raw_sizes;
  typedef sort<raw_sizes>::type sorted_raw_sizes;
  typedef unique<sorted_raw_vector, equal_to<_1, _2> >::type sizes;
  std::array<SizedFoos*, size<sizes>::type> foos_;
public:
  GeneralFoo() {
    ...
  }
  void Bar(size_t size) {
    //DOESN'T WORK: size is a runtime value
    //auto index = lower_bound<sizes, size>::type;
    auto index = ... //What goes here?
    foos_[index]->Bar();
  }
}

mpl是编译时-您试图实现的目标只能在运行时完成,因此需要使用std::lower_bound()