mardi 25 avril 2017

Tracking memory consumption of a template class

I have a class for which I'd like an estimation of how much memory it's consuming:

template <typename T, typename U>
class MyTemplateClass
{
    size_t EstimateMemoryConsumption() const
    {
        // the memory consumption is a function of the size of the first template argument
        return f(sizeof(T));
    }
}

And this works fine. Recently I've been asked to support T == std::string. The current implementation doesn't work because sizeof(std::string) doesn't reflect the memory consumption of an std::string (I need to call capacity() on the std::string). According to this, I cannot give a partial specialization for the member function (for T == std::string, and any U)

Is there a different way to achieve this?

Aucun commentaire:

Enregistrer un commentaire