I am attempting to implement a multi_index container of Boost::variant objects. The variant consists of two derived classes of one common base object. I have implemented a virtual function in each derived class ("extractKey()") which returns a std::pair<char,char> to provide a suitable key value regardless of which derived object occupies the variant.
How do I use apply_visitor() (perhaps in a lambda expression?) to invoke the "extractKey()" function as a CONST_MEM_FUN key extractor in order to obtain the key value? I have not been able to get the syntax correct in order to accomplish this.
I am using Visual Studio 2019 and C++17.
Edit: While I already have a much more sane and conventional solution which simply uses a container of base object pointers and virtual functions in the derived objects (and no variants!) other scenarios arise where there's a need to store fundamentally different objects (not derived from a common base object) in a multi_index container. That's the real reason why I'm hoping to find a solution to the question posed here.
The best way to approach this is to provide your own user-defined key extractor. Note that the fact that the variant types are derived from a common base does not play any significant role here: in a scenario without inheritance, simply apply a regular visitor that takes care of all the types in the variant (possibly with a generic lambda if all the types conform to the same syntax for getting the key).
Live Coliru Demo
Output