No no. However, you can achieve something similar for classes that support the .size
method, such as strings or a standard container:
template <class Key, class Value> unsigned long mapSize(const std::map<Key,Value> &map){ unsigned long size = sizeof(map); for(typename std::map<Key,Value>::const_iterator it = map.begin(); it != map.end(); ++it){ size += it->first.size(); size += it->second.size(); } return size; }
If you want to know the allocated memory, you can use .capacity
:
template <class Key, class Value> unsigned long mapCapacity(const std::map<Key,Value> &map){ unsigned long cap = sizeof(map); for(typename std::map<Key,Value>::const_iterator it = map.begin(); it != map.end(); ++it){ cap += it->first.capacity(); cap += it->second.capacity(); } return cap; }
source share