[multi_index] Question about 'modify'

I have a question about the 'modify' method of ordered indexes in the multi_index library. I have an iterator obtained from an ordered index and would like to modify the pointed element. I'm pretty sure that the modification won't change the order, won't create collisions and so on. The documentation suggests to use either 'replace', or more efficient 'modify' method. Why can't I do the following: typedef variables_t::nth_index<1>::type index_t; index_t& index = variable_declarations.get<1>(); index_t::iterator existing = index.find(vd); const_cast<Variable_declaration&>(*existing) .make_external(); ? TIA, Volodya

Vladimir Prus ha escrito:
I have a question about the 'modify' method of ordered indexes in the multi_index library. I have an iterator obtained from an ordered index and would like to modify the pointed element. I'm pretty sure that the modification won't change the order, won't create collisions and so on.
The documentation suggests to use either 'replace', or more efficient 'modify' method. Why can't I do the following:
typedef variables_t::nth_index<1>::type index_t; index_t& index = variable_declarations.get<1>(); index_t::iterator existing = index.find(vd); const_cast<Variable_declaration&>(*existing) .make_external();
You can do this as long as you're 100% sure there won't be collisions or reorderings. This is no different to const_casting away an element in a std::set: if you know what you're doing, well, it's OK. That said, using replace or modify keeps you on the safe side. Best, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

Joaquín Mª López Muñoz wrote:
The documentation suggests to use either 'replace', or more efficient 'modify' method. Why can't I do the following:
typedef variables_t::nth_index<1>::type index_t; index_t& index = variable_declarations.get<1>(); index_t::iterator existing = index.find(vd); const_cast<Variable_declaration&>(*existing) .make_external();
You can do this as long as you're 100% sure there won't be collisions or reorderings. This is no different to const_casting away an element in a std::set: if you know what you're doing, well, it's OK. That said, using replace or modify keeps you on the safe side.
OK. I started to wonder because the method above appears to be much more efficient than 'replace' and much more simple that 'modify', so I'm about to use it as default. - Volodya

Vladimir Prus ha escrito:
Joaquín Mª López Muñoz wrote:
The documentation suggests to use either 'replace', or more efficient 'modify' method. Why can't I do the following:
typedef variables_t::nth_index<1>::type index_t; index_t& index = variable_declarations.get<1>(); index_t::iterator existing = index.find(vd); const_cast<Variable_declaration&>(*existing) .make_external();
You can do this as long as you're 100% sure there won't be collisions or reorderings. This is no different to const_casting away an element in a std::set: if you know what you're doing, well, it's OK. That said, using replace or modify keeps you on the safe side.
OK. I started to wonder because the method above appears to be much more efficient than 'replace' and much more simple that 'modify', so I'm about to use it as default.
Well, the following index.modify( existing, boost::mem_fn(&Variable_declaration::make_external)); is not much more cumbersome, and it is certainly safer. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (2)
-
Joaquín Mª López Muñoz
-
Vladimir Prus