【问题标题】:boost mpl lower_bound at runtime在运行时提升 mpl lower_bound
【发布时间】:2013-11-20 21:21:34
【问题描述】:

在下面的代码中,我有一个类SizedFoo,它是为特定大小构造的。 GeneralFoo 类拥有一个不同大小的SizedFoos 数组,然后函数Bar 将找到SizedFoo,该SizedFoo 被初始化为大于传入参数的最小大小。我知道 boost mpl 具有 lower_bound 函数,但只能与静态已知值一起使用,有没有办法在运行时使用等价物?

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();
  }
}

【问题讨论】:

    标签: c++ boost c++11 metaprogramming


    【解决方案1】:

    mpl 是编译时间——你想要实现的只能在运行时完成,因为你需要使用std::lower_bound()

    【讨论】:

      猜你喜欢
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      相关资源
      最近更新 更多