0

I am applying example 6 of boost multi_index examples. https://www.boost.org/doc/libs/1_71_0/libs/multi_index/doc/examples.html#example6

I am still learning the syntax of key extraction.i find that member key extractor require for its third argument a pointer to class member of type type where class is first arg and type is second arg .

template<class Class,typename Type,Type Class::*PtrToMember>
struct member;  

But in example 6 the author uses 3rd arg which is not pointer .....

key_from_key< 
  member<car_manufacturer,
         const std::string, 
         &car_manufacturer::name>, 
  member<car_model, 
         const car_manufacturer *, 
         car_model::manufacturer> >

this should be & to make pointer which points to the member of class car_model of type const car_manufacturer * so giving us pointer to pointer .....but he just use the member identifiercar_model::manufacturer which is only pointer to member.
So why did the author omits the & from third argument??

if any more code required i will put it.

Joaquín M López Muñoz
  • 5,243
  • 1
  • 15
  • 20
ahmed allam
  • 377
  • 2
  • 15

1 Answers1

2

Congratulations, you found an error in Boost.MultiIndex docs :-)

It should be &car_model::manufacturer rather than car_model::manufacturer, as you correctly point out. What's more, car_model::manufacturer is not even legal C++, as, if anything, it would declare a reference to a member, which does not exist in the language.

Additionally, the docs say:

struct car_model
{
  std::string       model;
  car_manufacturer* manufacturer;
  int               price;
};

where it should be (as correctly written in the actual code):

struct car_model
{
  std::string             model;
  const car_manufacturer* manufacturer;
  int                     price;
};

Otherwise, member<car_model,const car_manufacturer *,&car_model::manufacturer> wouldn't match the type of the manufacturer member.

I'll fix all of this in the docs. Thanks for spotting the mess.

Joaquín M López Muñoz
  • 5,243
  • 1
  • 15
  • 20
  • i do not understand `car::manufacturer is not even legal C++` ?what is `car::manufacturer` do you mean `car_manufacturer`?and what difference do `const` do? i am still new to c++ .in the first struct ,`manufacturer` is pointer to `struct car_manufacturer` which is `non const`"to me this means it could be changed" and in code it is `pointer to const same struct` `car_manufacturer` So why the first is declaring `reference` "not pointer" and the second declaring `pointer`?.i read the link you gave and its `third point` is talking about `member of reference type`?where is the reference `&` sign?? – ahmed allam Nov 19 '19 at 04:36
  • Sorry I had a typo, I meant `car_model::manufacturer` rather han `car::manufacturer`. The rest of your message about `const`ness and pointers I'm afraid to say I don't understand. – Joaquín M López Muñoz Nov 19 '19 at 08:19
  • Thanks for ur reply,why does adding `const` make code works?in the first struct `car_model::manufacturer` is pointer to non const struct `car_manufacturer` type .But u said this is wrong and the right code is to add `const`.what do adding `const` do to make code legal c++? in the sentence`"it would declare a reference to a member" `do you mean by `reference` lvalue or rvalue reference which use `&` sign Or do you mean by `reference` a `pointer`???excuse my ignorance..thanks again.by the way your library is amazing once i could learn it – ahmed allam Nov 19 '19 at 11:04
  • 1
    What I mean is, if you have `struct X{some_type x;};` then `member`has to match the type of `x`, i.e, be of the form `member`. In the code, `manufacturer` is a `const car_manufacturer*`, matching with `member`. Dropping `const` on either place would result in a mismatch. – Joaquín M López Muñoz Nov 19 '19 at 12:01