i want to use map to save my data.
because my function is a member function, and i marked it as const.
So, when i want to get value, i can only use map.at(key)
but when the key is missing , i will get a const std::out_of_range & e
error.
For this case, i design a macro:
#include <map>
#include <stdio.h>
#include <string>
#define START_MAP try {
#define END_MAP(s) \
} catch (const std::out_of_range & e) {\
printf("%s map at crashed\n", s.c_str());\
exit(1);\
}\
int main() {
std::map<std::string, int> m;
START_MAP;
std::string b = "test";
int a = m.at(b);
END_MAP(""); // TODO: how can i get b here??
}
but i want to know the key name in my printf message, how can i get it?