modify_key() for a composite_key of a multi_index_container that contains a shared_ptr

I'd like to modify_key() for a composite_key of a multi_index_container that contains a shared_ptr.
I found a thread in this mailing list dealing with a similar kind of situation, not totally the same though: "[Boost-users] [multi_index] modify problem with composite_key"
My example:
This compiles fine:
struct entry
{
entry(int a, int b):m_x(a), m_y(b){}
int m_x;
int m_y;
};
typedef multi_index_container<
entry,
indexed_by<
ordered_unique<
composite_key<
entry,
member

Hello Zsolt, Zsolt Vajna wrote:
I'd like to modify_key() for a composite_key of a multi_index_container that contains a shared_ptr.
This compiles fine:
struct entry { entry(int a, int b):m_x(a), m_y(b){} int m_x; int m_y; };
typedef multi_index_container< entry, indexed_by< ordered_unique< composite_key< entry, member
, member >>>> C; void updateX(C::iterator iter,int x, C& m_c) { m_c.get<0>().modify( iter, (&ll::_1)->*&entry::m_x=x); }
My first problem is if I change modify to modify_key I get the following error: operator_lambda_func_base.hpp(237) : error C3892: 'boost::lambda::detail::select' : you cannot assign to a variable that is const
When you use modify_key, you modifier functor is passed *the key* rather than the whole element as is the case with modify. In this particular situation, the key is a composite key; so the key object (defined privately by composite_key) is some entity that somehow references entry::m_x and entry::m_y. This object is not exposed to the public and you're not supposed to try to use it or modify it, hence the problems you're seeing. In short, when using composite_key you cannot use modify_key.
My second problem is that instead of
typedef multi_index_container< entry, indexed_by< ordered_unique< composite_key< entry, member
, member >>>> C; i want to have a
typedef multi_index_container< shared_ptr<entry>, indexed_by< ordered_unique< composite_key< entry, member
, member >>>> C; Note the shared_ptr added.
In this later case I get another error:
boost\lambda\detail\member_ptr.hpp(512) : error C2440: 'newline' : cannot convert from 'const rt0 ' to 'entry *const '
As your elements are now shared_ptrs rather than plain entries, you need an additional dereferencing operation: void updateX(C::iterator iter,int x, C& m_c) { m_c.get<0>().modify( iter, (&*ll::_1)->*&entry::m_x=x); } (note the * before ll::_1). Lambda expressions can rapidly become unreadable, you might want to consider writing an adhoc functor for this. HTH, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -- View this message in context: http://www.nabble.com/modify_key%28%29-for-a-composite_key-of-a-multi_index_... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (2)
-
Joaquin M Lopez Munoz
-
Zsolt Vajna