devtk
devtk

Reputation: 2179

C++ class specialiation when dealing with STL containers

I'd like a function to return the size in bytes of an object for fundamental types. I'd also like it to return the total size in bytes of an STL container. (I know this is not necessarily the size of the object in memory, and that's okay).

To this end, I've coded a memorysize namespace with a bytes function such that memorysize::bytes(double x) = 8 (on most compilers).

I've specialized it to correctly handle std::vector<double> types, but I don't want to code a different function for each class of the form std::vector<ANYTHING>, so how do I change the template to correctly handle this case?

Here's the working code:

#include <iostream>
#include <vector>

// return the size of bytes of an object (sort of...)
namespace memorysize
{

  /// general object
  template <class T>
  size_t bytes(const T & object)
  {
    return sizeof(T);
  }

  /// specialization for a vector of doubles
  template <>
  size_t bytes<std::vector<double> >(const std::vector<double> & object)
  {
    return sizeof(std::vector<double>) + object.capacity() * bytes(object[0]);
  }

  /// specialization for a vector of anything???

}


int main(int argc, char ** argv)
{

  // make sure it works for general objects
  double x = 1.;
  std::cout << "double x\n";
  std::cout << "bytes(x) = " << memorysize::bytes(x) << "\n\n";

  int y = 1;
  std::cout << "int y\n";
  std::cout << "bytes(y) = " << memorysize::bytes(y) << "\n\n";

  // make sure it works for vectors of doubles
  std::vector<double> doubleVec(10, 1.);
  std::cout << "std::vector<double> doubleVec(10, 1.)\n";
  std::cout << "bytes(doubleVec) = " << memorysize::bytes(doubleVec) << "\n\n";

  // would like a new definition to make this work as expected
  std::vector<int> intVec(10, 1);
  std::cout << "std::vector<int> intVec(10, 1)\n";
  std::cout << "bytes(intVec) = " << memorysize::bytes(intVec) << "\n\n";

  return 0;
}

How do I change the template specification to allow for the more general std::vector<ANYTHING> case?

Thanks!

Upvotes: 2

Views: 198

Answers (1)

K-ballo
K-ballo

Reputation: 81409

Modified your code accordingly:

/// specialization for a vector of anything
template < typename Anything >
size_t bytes(const std::vector< Anything > & object)
{
  return sizeof(std::vector< Anything >) + object.capacity() * bytes( object[0] );
}

Note that now you have a problem if invoking bytes with an empty vector.

Edit: Scratch that. If I remember your previous question correctly, then if you get a vector of strings then you would like to take into account the size taken by each string. So instead you should do

/// specialization for a vector of anything
template < typename Anything >
size_t bytes(const std::vector< Anything > & object)
{
  size_t result = sizeof(std::vector< Anything >);

  foreach elem in object
       result += bytes( elem );

  result += ( object.capacity() - object.size() ) * sizeof( Anything ).

  return result;
}

Upvotes: 4

Related Questions