我希望能够boost::fusion::map
从两者中分配一个参考值:
- a
boost::fusion::map
的值,和 - 参考
boost::fusion::map
文献。
这样做的正确(通用和惯用)方法是什么?
// Let:
using bf = boost::fusion;
struct A {}; struct B{};
// I have a map of references
using my_ref_map = bf::map<bf::pair<A, float&>, bf::pair<B, int&>>;
// and a map of values:
using my_val_map = bf::map<bf::pair<A, float>, bf::pair<B, int>>;
// Say I have two values and I make a map of references
float a; int b;
my_ref_map MyRefMap(bf::make_pair<A>(a), bf::make_pair<B>(b));
// Then I wanto to set a and b using both:
// a map of values:
my_val_map MyValMap(bf::make_pair<A>(2.0), bf::make_pair<B>(3))
// and a map of references:
float aSrc = 2.0; int bSrc = 3;
my_ref_map MyRefMap(bf::make_pair<A>(aSrc), bf::make_pair<B>(bSrc));
// ... some code ... (see below for the things I've tried)
assert(a == 2.0 && b == 3); // <- End result.
我尝试了以下方法:
bf::copy(MyValMap, MyRefMap);
// copy complains that bf::pair<A, float&> cannot be assigned
// because its copy assignment operator is implicitly deleted.
// This is fine, I wasn't expecting copy to work here.
实现一个bf::zip_non_const
(见下文),允许您修改地图并执行以下操作:
bf::for_each(bf::zip_non_const(MyRefMap, MyValMap), [](auto i) {
bf::at_c<0>(i) = bf::at_c<1>(i); });
// This works but bf::zip returns const& for a reason:
// there has to be a better way.
这是我的实现zip_non_const
:
namespace boost { namespace fusion {
// Boilerplate:
namespace result_of {
template<class... Ts> struct zip_non_const {
using sequences = mpl::vector<Ts...>;
using ref_params
= typename mpl::transform<sequences, add_reference<mpl::_> >::type;
using type = zip_view<typename result_of::as_vector<ref_params>::type>;
};
}
// zip_non_const
template<class... Ts>
inline typename result_of::zip_non_const<Ts...>::type zip_non_const(Ts&&... ts)
{ return {fusion::vector<Ts&&...>(ts...)}; }
// swap for fusion types
template <class T> inline void swap(T&& lhs, T&& rhs) noexcept {
using std::swap;
std::remove_reference_t<T> tmp = lhs;
lhs = rhs;
rhs = tmp;
}
} // namespace fusion
} // namespace boost