map with custom comparison function? – C++ Forum

  1. Home
  2. vector maps manual
  3. map with custom comparison function? – C++ Forum

I would like to write a custom comparison function object that I can pass to a map container. In my hands, a function object works fine when passed to an algorithm like sort, but I can’t figure out how to use it in a container template. The compiler protests that I can’t pass it a non-constant type, ie I can’t use a constructor in the map declaration (line 35). Possibly I can find a workaround using function pointers but I suspect that will ultimately get ugly (bad style and safety). That’s great, works perfectly. Makes sense, too, in retrospect. Thanks so much for reading the post and taking the time to answer. When I go to use a map created this way, I run into problems if I pass the comparison constructor a variable (‘s’, line 26) rather than a constant parameter (-1, line 25). What’s going on? Maybe this is some subtle error concerning constructors, because I get a similar error when I use a map declared with an explicit argument-free comparison object (line 24). For another possible clue to the problem: I get a warning on the declaration of ‘s’ (line 21) that the variable is unused, even though I’ve clearly used it in passing it to my map (line 26). You are the victim of of a very very nasty problem inherited from the C grammar, which states something like ‘Anything that *might be* a declaration *is* a declaration’. In line 26 for example, you do in fact declare a function (surprise!) with the name mymap4 which returns a map&lt,int,int,compare_val&gt, and takes a compare_val with a formal parameter name of ‘s’ as argument. (Yes, ‘void foo(int x)’ is equivalent to ‘void foo(int(x))’) In line 24 the same problem exists, this time you declare a function named mymap2 which returns a map&lt,int,int,compare&gt, and takes as parameter a pointer to a function which returns a ‘compare’-object and takes no parameters. This ambiguity doesn’t exist in the other two cases. The first one is clear. In the second one, the formal parameter named ‘s’ would be named ‘-1’, which wouldn’t be legal. Thus the otherwise same statement cannot be a declaration, thus it must be an ‘expression-statement’ as defined in ISO 14882, i.e. the instantiation of the variable mymap3 of type map&lt,int,int,compare_val&gt, and a function pointer object of type compare_val constucted with the parameter -1, just like you expected. On a sidenote: the second problem wouldn’t have occured if you hadn’t circumvented the languages intended use of the namespace mechanism and had qualified the names with ‘std::’ instead of using the ‘using’ keyword, because formal parameter names cannot include the qualifier operator ‘::’. And yes, C++ indeed is a language you have to love. Otherwise you won’t get along with it. These subtleties keep you busy learning for a lifetime… Nope, the compiler does exactly what it is told – it declares a function and is done with it. This behavior is 100% standard conforming. Oh, the horror, the horror. That’s incredible. Thank you folks for the generous explanations and experiments on my behalf. Now I can interpret that bizarre-seeming error message, and hopefully I’ll recognize this most vexing parse in the future… Source.

whatsapp button