C++处理 STL 容器时的类专业化

发布于 2024-12-05 09:39:52 字数 1929 浏览 0 评论 0原文

我想要一个函数返回基本类型对象的大小(以字节为单位)。我还希望它返回 STL 容器的总大小(以字节为单位)。 (我知道这不一定是内存中对象的大小,没关系)。

为此,我使用 bytes 函数编写了一个 memorysize 命名空间,使得 memorysize::bytes(double x) = 8 (在大多数编译器)。

我已将其专门用于正确处理 std::vector 类型,但我不想为 std::vector形式的每个类编写不同的函数;,那么如何更改模板才能正确处理这种情况呢?

这是工作代码:

#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;
}

如何更改模板规范以允许更通用的 std::vector 情况?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2024-12-12 09:39:52

相应地修改您的代码:

/// 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] );
}

请注意,现在如果使用空的向量调用bytes,您会遇到问题。

编辑:从头开始。如果我正确地记得你之前的问题,那么如果你得到一个字符串向量,那么你会想要考虑每个字符串所占用的大小。所以你应该这样做

/// 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文