EDIT: This question is not a dupe. This is why...
I do not want to test:
1) if a given type is a map
(pseudocode: type=="map"
), but
2) if the type of a given variable is map
(pseudocode: typeof(var)=="map"
).
Having #1 (as in the linked question Checking if a type is a map) is not enough. One has to chain it with a function that gets the type
from the var
.
I did not find that, so far (typeid
does not seem to do the job).
Now the original question
I want to check if a given variable var
is a map
(regardless of the types of first
and second
), and without declaring any other variable.
Is this possible? How?
Note: I don't care about getting the human-readable form "map
", but only about getting a boolean for (pseudocode: typeof(var)=="map"
).
Is demangling (which is non-portable) the only way? Unmangling the result of std::type_info::name
Posting an answer here (inspired by https://stackoverflow.com/a/35293682/2707864, which solves the issue for a type, not a variable).
template<typename T>
constexpr bool is_map(const T&) { return false; }
template<typename Key, typename Value, typename Order, typename Allocator>
constexpr bool is_map(const std::map<Key, Value, Order, Allocator>&) {
return true;
}