[multiindex] key extractor as free function - how?
Hi! I read from the docs that we can define key extractors to be one of - identity - member - mem_fun - composite_key Is it possible to use a free function as key extractor in the same way as the ones listed above and can you give an example of this? reagrds, Markus
Markus Werle ha escrito:
Hi!
I read from the docs that we can define key extractors to be one of - identity - member - mem_fun - composite_key
Is it possible to use a free function as key extractor in the same way as the ones listed above and can you give an example of this?
Hello Markus,
There is no *predefined* key extractor designed for use with free
functions, but you can easily write your own, as sketched in the
following:
struct element{...};
// free function to be used for key extraction
int get_value(const element& x);
// associated key extractor
struct get_value_extractor
{
typedef int result_type;
result_type operator()(const element& x)const
{
return get_value(x);
}
};
typedef multi_index_container<
element,
indexed_by<
ordered_unique
multi_t;
User-defined key extractors are explained at http://tinyurl.com/l3g6u . On the other hand, it might be a good idea to provide an additional predefined key extractor for use with free functions so as to supplement the existing ones working with member functions, I'll add this to my to consider list. Hope this helps, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Joaquín Mª López Muñoz
User-defined key extractors are explained at http://tinyurl.com/l3g6u .
[Easy archive reading at times when tinyurl vanished: Joaquín means http://boost-consulting.com/boost/libs/multi_index/ doc/tutorial/key_extraction.html#user_defined_key_extractors] I apologize for not reading the (really good) docs carefully enough. Thanks for your answer.
On the other hand, it might be a good idea to provide an additional predefined key extractor for use with free functions so as to supplement the existing ones working with member functions, I'll add this to my to consider list.
This would be fine. Markus
Joaquín Mª López Muñoz
[solution]
Unfortunately your solution does not work for tuples.
The following code fails with a 10-page error message with gcc:
#include
Markus Werle
Joaquín Mª López Muñoz
writes: [solution]
Unfortunately your solution does not work for tuples. The following code fails with a 10-page error message with gcc:
#include
#include #include #include
template error messages are somewhat misleading ... finally caught it:
#include
Markus Werle ha escrito:
Markus Werle
writes: JoaquÃn Mª López Muñoz
writes: [solution]
Unfortunately your solution does not work for tuples. The following code fails with a 10-page error message with gcc:
#include
#include #include #include template error messages are somewhat misleading ... finally caught it: #include
was missing Markus
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Additionally to this solution, I explain other options at my other post, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
On 1/23/07, Joaquín Mª López Muñoz
it might be a good idea to provide an additional predefined key extractor for use with free functions so as to supplement the existing
ones working with member functions, I'll add this to my to consider list.
Hope this helps,
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
I'm currently experimenting with a rule of thumb where my extensible
template classes are all written using policies, but the default for each
policy is one that calls a free function. Sort of a best of both worlds
approach - or at least that's what I'm hoping. For example, intrusive_ptr
could be rewritten to take an AddRef policy, and the default could be to
call intrusive_ptr_add_ref, etc:
template <typename T>
struct intrusive_ptr_default_add_ref_policy
{
static void add_ref(T * t)
{
intrusive_ptr_add_ref(t);
}
};
template
participants (3)
-
Gottlob Frege
-
Joaquín Mª López Muñoz
-
Markus Werle