Boost logo

Boost Users :

Subject: Re: [Boost-users] modify_key() for a composite_key of a multi_index_container that contains a shared_ptr
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2009-10-04 13:12:50


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<entry,int,&entry::m_x>,
> member<entry,int,&entry::m_y>>>>> 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<entry,int,&entry::m_x>,
> member<entry,int,&entry::m_y>>>>> C;
>
> i want to have a
>
> typedef multi_index_container<
> shared_ptr<entry>,
> indexed_by<
> ordered_unique<
> composite_key<
> entry,
> member<entry,int,&entry::m_x>,
> member<entry,int,&entry::m_y>>>>> 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_container-that-contains-a-shared_ptr-tp25718379p25739665.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net