[英]ambiguous map.insert in boost
I am trying to insert a values into the boost map but the call to the insert statement is ambiguous in boost map insert. 我正在尝试将值插入到Boost映射中,但是在boost映射插入中对insert语句的调用不明确。
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <utility>
using namespace boost::interprocess;
int main ()
{
// remove earlier existing SHM
shared_memory_object::remove("SharedMemoryName");
// create new
managed_shared_memory segment(create_only,"SharedMemoryName",65536);
//Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
//so the allocator must allocate that pair.
typedef std::string KeyType;
typedef map<std::string, int> MappedType;
typedef std::pair<const KeyType, MappedType> ValueType;
//allocator of for the map.
typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
//Initialize the shared memory STL-compatible allocator
ShmemAllocator alloc_inst (segment.get_segment_manager());
//third parameter argument is the ordering function is used to compare the keys.
typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MySHMMap;
//offset ptr within SHM for map
offset_ptr<MySHMMap> m_pmap = segment.construct<MySHMMap>("MySHMMapName")(std::less<std::string>(), alloc_inst);
//Insert data in the map
std::string my_string = "test";
m_pmap[0].insert(std::make_pair( my_string, 0) );
return 0;
}
The boost APIs that get called are: 被调用的boost API是:
std::pair<iterator,bool> insert(const nonconst_value_type& x)
std::pair<iterator,bool> insert(const value_type& x)
typedef typename tree_t::value_type value_type;
typedef std::pair<key_type, mapped_type> nonconst_value_type;
Error Log: 错误日志:
/home/user/droy/src/quotes/shmmutimap/src/writer.cxx:39: error: call of overloaded 'insert(std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>)' is ambiguous
/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/map.hpp:410: note: candidates are: std::pair<typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::iterator, bool> boost::container::map<Key, T, Pred, Alloc>::insert(const typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::value_type&) [with Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T = boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >, Pred = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, Alloc = boost::interprocess::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]
/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/map.hpp:421: note: std::pair<typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::iterator, bool> boost::container::map<Key, T, Pred, Alloc>::insert(const std::pair<typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::key_type, T>&) [with Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T = boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >, Pred = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, Alloc = boost::interprocess::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]
The parameters to the insert statement end up to be the same type,how could I get around this problem, any hints? 插入语句的参数最终变为同一类型,如何解决这个问题,有任何提示吗?
Right now, you create a map<string, map<string, int>>
. 现在,您创建一个
map<string, map<string, int>>
。 I imagine that your intention was to create just a map<string, int>
. 我想您的意图只是创建一个
map<string, int>
。 To do that, you need a very simple fix to your code: 为此,您需要对代码进行非常简单的修复:
// typedef map<std::string, int> MappedType;
typedef int MappedType;
As an aside, you could also write the last line of your code as: 顺便说一句,您还可以将代码的最后一行编写为:
// m_pmap[0].insert(std::make_pair( my_string, 0) );
m_pmap[0][my_string] = 0;
As it appears that my intuition was wrong, and you did want to create a map<string, map<string, int>>
, then the response is slightly different. 似乎我的直觉是错误的,并且您确实想创建
map<string, map<string, int>>
,因此响应略有不同。
Note that your call is basically attempting to do this: 请注意,您的通话基本上是尝试执行此操作:
map<string, int> x(0);
m_pmap[0][my_string] = x;
Note that there is no constructor for map<string, int>
that takes an integer. 请注意,没有
map<string, int>
构造函数采用整数。 Instead, you could do any of: 相反,您可以执行以下任一操作:
m_pmap[0].insert(std::make_pair( my_string, map<std::string, int>()));
m_pmap[0].insert(std::make_pair( my_string, MappedType()));
m_pmap[0][my_string] = map<std::string, int>();
m_pmap[0][my_string] = MappedType();
You could also simply take advantage of how the map
works, and do: 您还可以简单地利用
map
工作原理,并执行以下操作:
m_pmap[0]["us"]["abc"] = 2030;
m_pmap[0]["us"]["def"] = 1230;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.