gpt4 book ai didi

c++ - 从值/引用映射分配给引用映射

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:25:47 26 4
gpt4 key购买 nike

我希望能够分配给一个 boost::fusion::map 来自以下两个方面的引用值:

  • 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(见下文),允许您修改 map 并执行以下操作:

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

最佳答案

fusion::map定义一个 =允许赋值的运算符。这在您分配 my_val_map 的情况下工作正常到 my_ref_map , 但失败并出现与 fusion::copy 相同的错误当分配在两个 my_ref_map 之间时秒。在下面的代码中,我简单地遍历了第一个 map 中的对,然后将数据分配给目标 map 中的相应对。
重要的是目标映射具有从中复制它的映射中存在的所有键,否则您将遇到编译错误。 (如果您有 Map1=map<pair<A,int> >; Map2=map<pair<A,int>, pair<B,float> >;,您可以从 Map1 复制到 Map2,但不能从 Map2 复制到 Map1)。
Live Example

#include <iostream>

#include <boost/fusion/include/map.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/at_key.hpp>

namespace fusion = boost::fusion;
struct A {}; struct B{};

// I have a map of references
using my_ref_map = fusion::map<fusion::pair<A, float&>, fusion::pair<B, int&>>;

// and a map of values:
using my_val_map = fusion::map<fusion::pair<A, float>, fusion::pair<B, int>>;

template<typename MapOut>
struct map_assigner //you could use a c++14 lambda if your compiler supports it
{
map_assigner(MapOut& map):map_out(map){}

template <typename Pair>
void operator()(const Pair& pair) const
{
fusion::at_key<typename Pair::first_type>(map_out) = pair.second;
}

MapOut& map_out;
};

template <typename MapIn, typename MapOut>
void my_copy(const MapIn& map_in, MapOut& map_out)
{
fusion::for_each(map_in,map_assigner<MapOut>(map_out));
}


int main()
{

// Say I have two values and I make a map of references
float a=0.0f;
int b=0;
my_ref_map MyRefMap(fusion::make_pair<A,float&>(a), fusion::make_pair<B,int&>(b));

// Then I wanto to set a and b using both:
// a map of values:
my_val_map MyValMap(fusion::make_pair<A>(2.0f), fusion::make_pair<B>(3));

// and a map of references:
float aSrc = 4.0f; int bSrc = 6;
my_ref_map MyRefMap2(fusion::make_pair<A,float&>(aSrc), fusion::make_pair<B,int&>(bSrc));

my_copy(MyValMap,MyRefMap);

std::cout << "a=" << a << ", b=" << b << std::endl; // <- End result.

my_copy(MyRefMap2,MyRefMap);

std::cout << "a=" << a << ", b=" << b << std::endl; // <- End result.
}

关于c++ - 从值/引用映射分配给引用映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19540291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com