I am trying to iterate over a multimap<int, weak_ptr<RenderObject>>
but for some reason using the for loop below causes an error.
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..
}
The error visual studio complains about is l-value specifies const object
. After looking through the error output in vs I think the problem is from the weak_ptr<>
as if I try the same thing but with a shared_ptr
as the value in the map there are no errors.
My question is if my hypothesis is correct that the weak_ptr<>
is causing the error what is the reason/explanation for this behaviour?
I could always use a list of weak_ptr as this does not seem to have the same issue but I want to have the RenderObject
sorted based on render order and a map gives this by default whereas a list would require a resort on each insertion/change/removal which would be very inefficient.
Console Output:
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 ==========
EDIT
I have tried for(const auto& renderPair : m_render_objects)
and for(auto& renderPair : m_render_objects)
. Also for and while loop variations using the iterator. All of which produce the exact same error.
NOTE
I am using Visual Studio 2017 with C++17.