简体   繁体   中英

Getting rid of references in an boost::fusion sequence

I'm trying to use Boost::Fusion to transform a list of function's parameter types into a fusion::list. Ultimately, I am trying to turn a list of variables into parameters that I can call a function with (http://stackoverflow.com/questions/11164914/generating-wrappings-for-c-functions).

I've gotten this to work for non-referenced variables. However, it fails to compile for non-referenced variables when I try to turn the function's parameter list (specifically on the fusion::to_list it complains it can't deref the iterator).

I've simplified the code down by a bit below:

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

If foo is defined as:

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

it works fine, but it breaks on:

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

For the purposes of my code, I don't actually need the references kept in the sequence, which I am assuming is the issue (also, searches I've done tend to point that as being the culprit). However, I'm not completely sure of how to strip the transformed function of these references before as_list is called.

I tried something along the lines of:

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

but got the same compile errors.

Any ideas of how to fix this?

update

Here is the truncated compiler error I get (with clang++ --std=c++0x) for both cases given above:

/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);

If your compiler is C++11 compatible, you might want to look into the `std::remove_reference function. Or at least try to find an implementation of it and use as reference for making your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM