c++ - Is it possible to use boost.any as a key in a std::map(or something similiar)? -
std :: map & lt; Any, string & gt; is not working, so I wonder if there is another way I think this issue boost :: any code>, but with the fact that you are not specifying a custom comparator since map is a sorted associate container, therefore you need a comparator.
Do the following for me: tailor it according to your purpose:
# include & lt; Iostream & gt; # Include & lt; Map & gt; #include & lt; Boost / any.hpp & gt; using namespace std; Struct cmp {bool operator () (Const. Boost :: any & amp; l, const boost :: any & amp; r) {try {int left = boost :: any_cast & lt; Int & gt; (L); Int right = boost: any_cast & lt; Int & gt; (R); Return left & lt; right; } Hold (const boost :: bad_any_cast & amp;) {return false; } return false; }}; Int main () {map & lt; Boost :: any, string, cmp & gt; mother; Promotion :: Any to_append = 42; Ma.insert (std :: make_pair (to_append, "int")); If (ma.find (42)! = Ma.end ()) {cout & lt; & Lt; "Hooray \ n"; } Return 0; }
Comments
Post a Comment