0
votes

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.

Try for (auto& renderPair : m_render_objects) - user9212993
@user9212993 I have actually already tried that. I produces the exact same error. I have updated my question to include my previous attempts to resolve the error - Matthew
Provide a minimal reproducible example that reproduces your exact error please! - user9212993
@user9212993 I do not see how this is not a minimal, complete, and verifiable example. It provides the error output, the code that causes the problem and the required class definitions. I could add a tone more information but then that would not be minimal. Please give more information as to what you need to help. - Matthew
As said above, please provide a minimal, complete and verifiable example. Such an example is one that I can simply copy-paste it on my machine and compile it. Currently your snippets don't fulfill this. I need to put them together and in the process try to figure what headers do I need to include(e.g. what is STRCODE and where is it declared). If you have dependencies on non-standard headers, change your class to something dummy in order to remove dependencies, this may actually may help you find the error. I hope now you see why your code is not minimal, complete and verifiable. - opetroch