【问题标题】:Getting rid of references in an boost::fusion sequence摆脱 boost::fusion 序列中的引用
【发布时间】:2012-06-26 02:22:06
【问题描述】:

我正在尝试使用 Boost::Fusion 将函数的参数类型列表转换为 fusion::list。最终,我试图将变量列表转换为可以调用函数的参数(http://stackoverflow.com/questions/11164914/generating-wrappings-for-c-functions)。

我已经让它适用于非引用变量。但是,当我尝试打开函数的参数列表时(特别是在 fusion::to_list 上它抱怨它无法取消引用迭代器)时,它无法编译非引用变量。

我已将代码简化如下:

struct identity {
  template<typename Sig> struct result;

  template <typename T>
  struct result<convert(T)> { typedef T type; };

  template <typename T>
  typename T operator ()(T) const { 
     return T();
  }
};

int main(int argc, char **argv) {
  typedef BOOST_TYPEOF(foo) params_type;
  auto seq = function_types::parameter_types<params_type>();
  auto transformed = fusion::transform(seq, identity());
  auto passedParams = fusion::as_list(transformed);
}

如果 foo 被定义为:

int foo(int a) { return 5*a; }

它工作正常,但它会中断:

int foo(int &a) { return 5*a; }

就我的代码而言,我实际上并不需要保留在序列中的引用,我认为这是问题所在(此外,我所做的搜索倾向于指出这是罪魁祸首)。但是,我不完全确定在调用as_list 之前如何去除这些引用的transformed 函数。

我尝试了一些类似的方法:

template <typename T>
struct result<convert(T)>: remove_reference<T> {};

template <typename T>
typename remove_reference<T>::type operator ()(remove_reference<T>::type) const { return typename remove_reference<T>::type(); }

但得到相同的编译错误。

关于如何解决这个问题的任何想法?

更新

这是我在上面给出的两种情况下得到的截断编译器错误(使用 clang++ --std=c++0x):

/usr/local/include/boost/fusion/adapted/mpl/mpl_iterator.hpp:43:24: error: 
      reference to type 'int' requires an initializer
                return type();
                       ^
/usr/local/include/boost/fusion/iterator/deref.hpp:61:28: note: in instantiation
  of member function
  'boost::fusion::mpl_iterator<boost::mpl::v_iter<boost::function_types::parameter_types<void
  (int &), boost::add_reference<mpl_::arg<-1> > >, 0>
  >::deref<boost::fusion::mpl_iterator<boost::mpl::v_iter<boost::function_types::parameter_types<void
  (int &), boost::add_reference<mpl_::arg<-1> > >, 0> > >::call' requested
  here
    return deref_meta::call(i);

...

test4.cpp:65:22: note: in instantiation of function template specialization
  'boost::fusion::as_list<boost::fusion::transform_view<const
  boost::function_types::parameter_types<void (int &),
  boost::add_reference<mpl_::arg<-1> > >, convert, boost::fusion::void_> >'
  requested here
    auto passedParams = fusion::as_list(transformed);

【问题讨论】:

  • 如果您遇到编译错误,最好编辑您的问题以包含它们。
  • 会做——我避免这样做,因为它是模板错误,因此很长且难以解析。因此,我在(上面)包含了编译器给出的最终错误(在 deref_impl 中调用 as_list 时)。
  • 好的,我添加了错误的缩写形式。如果有帮助,我可以包括更多,跟踪贯穿: defer.hpp -> defer_impl.hpp -> defer.hpp -> build_cons.hpp -> convert.hpp -> test4.cpp

标签: c++ templates boost reference boost-fusion


【解决方案1】:

如果您的编译器与 C++11 兼容,您可能需要查看 `std::remove_reference 函数。或者至少尝试找到它的实现并用作自己制作的参考。

【讨论】:

  • 我确实尝试过 remove_reference(见上文),但可能没有正确应用它。你能举个例子把它应用到融合视图吗?
猜你喜欢
  • 2010-11-11
  • 2014-06-11
  • 1970-01-01
  • 2011-01-31
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
相关资源
最近更新 更多