0

I am trying to use std::map to map numerical IDs to function handlers: All function handlers take two integers and return an integer. Handler 100 would calculate the product.

When preparing the map, the compiler throws prog.cc:16:17: error: no matching function for call to 'std::map<int, int (*)(int, int)>::insert(int, int (*)(int, int))'. Please note the identical signature (int, int (*)(int, int)) on both sides of the error message.

My questions are,

  1. Is it a sound approach to use std::map for the described purpose?

  2. Why does the sample code throw an error, and obviously

  3. How to correct the code?

Although having >40y of programming experience, I am quite new to C++ and for sure I am missing some basic aspect here. Can you please point me into the right direction?

Thanks!

#include <map>

typedef int (*t_handler)(int,int);
// Gives the same result: using t_handler = int (*)(int,int);

int product(int u, int v) {
  return u*v;
}

// Map numeric ID to a function
std::map<int, t_handler> myMap;

int main() {
    // Define 100 as the function to calculate the product
    myMap.insert(100, &product);    // prog.cc:16:17: error: no matching function for call to 'std::map<int, int (*)(int, int)>::insert(int, int (*)(int, int))'

    // Calculate 5*8
    int x = myMap[100](5, 8);
    return 0;
}
4
  • 1
    "My questions are" - Please ask only one question per SO question. Commented Nov 4 at 18:48
  • I'd suggest reading the interface for insert again, with particular attention given to the argument type. The error message is telling you that the function you are trying to call doesn't exist. It's a little opaque because there are a bunch of overloads for insert, some of which take two arguments. Commented Nov 4 at 18:50
  • Using myMap[100]=&product; solved the issue - thank you so much! Regarding other input: This is a very much simplified example. In real world, the keys are even 64 bit, but sparsely used (only abount 100). Thanks anyway for the good thought! Commented Nov 5 at 9:06
  • Offtopic: std::unordered_map<int, std::function<int(int, int)>> should be more handy. Commented Nov 5 at 9:43

1 Answer 1

2

Check out https://en.cppreference.com/w/cpp/container/map/insert.html -- there is no "insert" with the signature that you want.

You'll probably want to use something like myMap[100]=&product;

if you want to use 'insert', note that the map's value_type is a std::pair. So, that can be inserted.


As a side note, I don't know what your numbers mean. However, if they are all small integers, consider using a vector (and just store nullptr for numbers you don't need). If they have meaning, consider using an enum so the meaning becomes visible.

Sign up to request clarification or add additional context in comments.

2 Comments

Or myMap.insert(std::make_pair(100, &product)); (which would require including <utility>).
or in c++11 just myMap.insert({100, &product});. Given std::map heavily depends on std::pair I doubt many standard libraries don't include <utility> via <map> already

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.