迭代multimap <int,weak_ptr <=“”>&gt;

时间:2018-01-21 16:35:56

标签: c++ iterator multimap weak-ptr

我正在尝试迭代multimap<int, weak_ptr<RenderObject>>,但出于某种原因,使用下面的for循环会导致错误。

using RenderIndex = int32_t;
class RenderObject
{
public:
   RenderIndex m_render_index;
   // rest of class definition
};

std::multimap<RenderIndex, std::weak_ptr<RenderObject>> m_render_objects;
for (auto renderPair : m_render_objects) // error occurs here
{
    // call render function..
}

visual visual抱怨的错误是l-value specifies const object。在查看vs中的错误输出后,我认为问题来自weak_ptr<>,好像我尝试了同样的事情,但是地图中的值shared_ptr没有错误。

我的问题是,如果我的假设是正确的,weak_ptr<>导致错误是什么原因/这种行为的解释?

我总是可以使用weak_ptr列表,因为这似乎没有相同的问题,但我想根据渲染顺序对RenderObject进行排序,并且默认情况下地图会给出这个,而列表则需要在每次插入/更换/移除时采用非常低效的方式。

控制台输出:

1>------ Build started: Project: Engine, Configuration: Debug x64 ------
1>RenderLayer.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\utility(246): error C2166: l-value specifies const object
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\utility(245): note: while compiling class template member function 'std::pair<const _Kty,_Ty> &std::pair<const _Kty,_Ty>::operator =(std::pair<const _Kty,_Ty> &&) noexcept(false)'
1>        with
1>        [
1>            _Kty=otw::RenderIndex,
1>            _Ty=std::weak_ptr<otw::RenderObject>
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\algorithm(1325): note: see reference to function template instantiation 'std::pair<const _Kty,_Ty> &std::pair<const _Kty,_Ty>::operator =(std::pair<const _Kty,_Ty> &&) noexcept(false)' being compiled
1>        with
1>        [
1>            _Kty=otw::RenderIndex,
1>            _Ty=std::weak_ptr<otw::RenderObject>
1>        ]
1>c:\outlaw\outlaw_games_engine\engine\renderlayer.cpp(13): note: see reference to class template instantiation 'std::pair<const _Kty,_Ty>' being compiled
1>        with
1>        [
1>            _Kty=otw::RenderIndex,
1>            _Ty=std::weak_ptr<otw::RenderObject>
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\xstring(2007): note: see reference to function template instantiation 'std::_String_alloc<std::_String_base_types<_Elem,_Alloc>>::_String_alloc<const _Alloc&,void>(_Any_alloc)' being compiled
1>        with
1>        [
1>            _Elem=wchar_t,
1>            _Alloc=std::allocator<wchar_t>,
1>            _Any_alloc=const std::allocator<wchar_t> &
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\xstring(2006): note: see reference to function template instantiation 'std::_String_alloc<std::_String_base_types<_Elem,_Alloc>>::_String_alloc<const _Alloc&,void>(_Any_alloc)' being compiled
1>        with
1>        [
1>            _Elem=wchar_t,
1>            _Alloc=std::allocator<wchar_t>,
1>            _Any_alloc=const std::allocator<wchar_t> &
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\string(516): note: see reference to function template instantiation 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>::basic_string<_Elem*,void>(_Iter,_Iter,const _Alloc &)' being compiled
1>        with
1>        [
1>            _Elem=wchar_t,
1>            _Iter=wchar_t *,
1>            _Alloc=std::allocator<wchar_t>
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\string(516): note: see reference to function template instantiation 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>::basic_string<_Elem*,void>(_Iter,_Iter,const _Alloc &)' being compiled
1>        with
1>        [
1>            _Elem=wchar_t,
1>            _Iter=wchar_t *,
1>            _Alloc=std::allocator<wchar_t>
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\string(596): note: see reference to function template instantiation 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> std::_Integral_to_string<wchar_t,int>(const _Ty)' being compiled
1>        with
1>        [
1>            _Ty=int
1>        ]
1>Done building project "Engine.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

修改

我尝试了for(const auto& renderPair : m_render_objects)for(auto& renderPair : m_render_objects)。也适用于使用迭代器的while循环变量。所有这些都会产生完全相同的错误。

我正在使用Visual Studio 2017和C ++ 17。

0 个答案:

没有答案
相关问题