问题描述
以下示例演示了我的意思:
The following example demonstrates what I mean:
#include <boost/mpl/map.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/pair.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/insert.hpp>
#include <iostream>
using namespace boost::mpl;
template <int n1, int n2>
struct entry
{
typedef pair<int_<n1>, int_<n2> > type;
};
typedef map<entry<1,1>::type> entries;
typedef insert<
entries, entry<4,4>::type>::type update;
typedef insert<
update,
entry<5,5>::type>::type update2;
struct print_values
{
template <class I>
void operator()(I)
{
std::cout << first<I>::type::value << ", "
<< second<I>::type::value << std::endl;
}
};
int main()
{
for_each<update2>(print_values());
std::cout << "Next:" << std::endl;
for_each<update2::type>(print_values());
}
输出:
1, 1
4, 4
5, 5
Next:
1, 1
当我通过访问 update2 :: type评估
我插入的项目消失了。 update2
时
When I evaluate update2
by accessing update2::type
the items I inserted disappear.
为什么会发生这种情况,我该怎么做才能确保评估的 update2
不会删除插入的元素吗?
Why does this happen and what can I do to to make sure the evaluating update2
doesn't remove the inserted elements?
推荐答案
我确信这是<$ c中的错误$ c> boost :: mpl 。
在 mpl :: map中插入元素的结果
不是 mpl :: map
类型的项目,而是 mpl :: m_item
类型的项目。此 m_item
包含新插入的键和值以及它们插入到的映射。
现在, boost中的每个类:: mpl
是一个元函数,容器是一个返回自身的元函数。
The result of inserting an element into an mpl::map
is not an item of type mpl::map
but of mpl::m_item
. This m_item
holds the newly inserted key and value as well as the map they were inserted into.
Now every class in boost::mpl
is a metafunction, containers are a metafunction that return themselves. This is useful for situations like this
typedef map<pair<int,int> > i_map;
eval_if<true_,
i_map,
at<i_map, float> >::type type;
所以 mpl :: map
具有typedef在其中只是 typedef映射类型;
, m_item
,但是缺少此 typedef
,因此由于它是从映射
派生而来的,因此它被插入到 m_item :: type
实际上是 map :: type
。
So mpl::map
has a typedef inside it that is simply typedef map type;
, m_item
, however, lacks this typedef
and so since it derives from the map
it's inserted into m_item::type
is actually map::type
.
这表示插入后的示例(删除 int_<> ;为简洁起见,
)如下:
This means my example after insertion (dropping the int_<>
for brevity) looks like:
m_item<5, 5, m_item<4, 4, map1<pair<1, 1> > > >
并访问其类型一直返回到 map1< pair< 1、1> > :: type
返回 map< pair< 1、1> >
,这就是所有插入的项目都消失的原因。
and accessing its type reaches all the way back up to map1<pair<1, 1> >::type
which returns map<pair<1, 1> >
which is why all the inserted items are disappearing.
我登录了,最初在发布此答案之前等待响应,但是4周后,我决定在没有任何回答的情况下发布此内容。
I logged a bug and was originally waiting for a response before posting this answer but after 4 weeks I decided to post this without any answer from them.
解决方案是简单地添加 typedef m_item type;
到 boost :: mpl :: m_item
在 boost / mpl / map / aux_ / item.hpp
中。这将使 m_item
成为可正确返回自身而不返回原始地图的元函数。
The solution is to simply add typedef m_item type;
to boost::mpl::m_item
in boost/mpl/map/aux_/item.hpp
. This will make m_item
a metafunction that correctly returns itself and not return the original map.
这篇关于插入的项目从boost :: mpl :: map中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!