C++:ptr_multimap::insert 问题

发布于 2024-07-23 11:27:38 字数 2364 浏览 9 评论 0原文

请考虑以下代码:

#include "boost/ptr_container/ptr_map.hpp"

int main()
{
        typedef boost::ptr_multimap<char, const int> M;
        M m;
        char c = 'c';
        int* j = new int(7);
        m.insert(c, j);
        return 0;
}

gcc 4.3.3 编译器无法编译上述代码。 我插入非常量是不是做错了什么? 我不能在多重映射中存储指向 const 的指针吗?

try.cpp: In function ‘int main()’:
try.cpp:8: error: expected primary-expression before ‘=’ token
/usr/include/c++/4.3/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = char, _U2 = const int*, _T1 = const char, _T2 = void*]’:
boost/boost_1_39_0/boost/ptr_container/ptr_map_adapter.hpp:765:   instantiated from ‘typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::iterator boost::ptr_multimap_adapter<T, VoidPtrMultiMap, CloneAllocator, Ordered>::insert_impl(const typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::key_type&, typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::mapped_type) [with T = const int, VoidPtrMultiMap = std::multimap<char, void*, std::less<char>, std::allocator<std::pair<const char, void*> > >, CloneAllocator = boost::heap_clone_allocator, bool Ordered = true]’
boost/boost_1_39_0/boost/ptr_container/ptr_map_adapter.hpp:799:   instantiated from ‘typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::iterator boost::ptr_multimap_adapter<T, VoidPtrMultiMap, CloneAllocator, Ordered>::insert(typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::key_type&, typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::mapped_type) [with T = const int, VoidPtrMultiMap = std::multimap<char, void*, std::less<char>, std::allocator<std::pair<const char, void*> > >, CloneAllocator = boost::heap_clone_allocator, bool Ordered = true]’
try.cpp:9:   instantiated from here
/usr/include/c++/4.3/bits/stl_pair.h:106: error: invalid conversion from ‘const void*’ to ‘void*’

我也尝试过,但在插入之前使用 const 转换 j 失败了。

Please consider the following code:

#include "boost/ptr_container/ptr_map.hpp"

int main()
{
        typedef boost::ptr_multimap<char, const int> M;
        M m;
        char c = 'c';
        int* j = new int(7);
        m.insert(c, j);
        return 0;
}

The gcc 4.3.3 compiler fails to compiler the above code. Am I doing something wrong by inserting a non-const? Can't I store a pointer to const in the multimap?

try.cpp: In function ‘int main()’:
try.cpp:8: error: expected primary-expression before ‘=’ token
/usr/include/c++/4.3/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = char, _U2 = const int*, _T1 = const char, _T2 = void*]’:
boost/boost_1_39_0/boost/ptr_container/ptr_map_adapter.hpp:765:   instantiated from ‘typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::iterator boost::ptr_multimap_adapter<T, VoidPtrMultiMap, CloneAllocator, Ordered>::insert_impl(const typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::key_type&, typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::mapped_type) [with T = const int, VoidPtrMultiMap = std::multimap<char, void*, std::less<char>, std::allocator<std::pair<const char, void*> > >, CloneAllocator = boost::heap_clone_allocator, bool Ordered = true]’
boost/boost_1_39_0/boost/ptr_container/ptr_map_adapter.hpp:799:   instantiated from ‘typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::iterator boost::ptr_multimap_adapter<T, VoidPtrMultiMap, CloneAllocator, Ordered>::insert(typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::key_type&, typename boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator, Ordered>::mapped_type) [with T = const int, VoidPtrMultiMap = std::multimap<char, void*, std::less<char>, std::allocator<std::pair<const char, void*> > >, CloneAllocator = boost::heap_clone_allocator, bool Ordered = true]’
try.cpp:9:   instantiated from here
/usr/include/c++/4.3/bits/stl_pair.h:106: error: invalid conversion from ‘const void*’ to ‘void*’

I also tried but failed with const casting j before insertion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七月上 2024-07-30 11:27:38

这似乎是问题所在。 它可以在模板参数中没有 const 的情况下正常编译。

That seems to be the problem. It compiles fine without the const in the template parameters.

朮生 2024-07-30 11:27:38

据我所知,这似乎是 boost 库中的一个错误。

但我不明白为什么你会希望你的 int* 是 const 。 您将此项目的内存管理委托给 ptr 容器。 为它提供非常量的物品才是公平的。 如果您不希望此容器的用户检索指针并修改它,我建议您插入一个类层以仅返回 const 元素。

From what i can tell, it seems to be a bug in the boost library.

But i don't understand why you would want your int* to be const. You're delegating the management of the memory for this item to the ptr container. It's only fair to provide it with non-const items. If you don't want users of this container to retrieve the pointer and modify it, i suggest you insert a class layer to only return const elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文