[Boost-users] modify_key() for a composite_key of a multi_index_container that contains a shared_ptr

74 views
Skip to first unread message

Zsolt Vajna

unread,
Oct 2, 2009, 3:00:07 AM10/2/09
to boost...@lists.boost.org
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<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

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 '

I assume I need to change something around the _1 bit, but have no idea what.

Thanks



_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Joaquin M Lopez Munoz

unread,
Oct 4, 2009, 1:12:50 PM10/4/09
to boost...@lists.boost.org

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.

Reply all
Reply to author
Forward
0 new messages