boost compressedPair

boost::compressed_pair behaves like std::pair. However, if one or both template parameters are empty classes, boost::compressed_pair consumes less memory. boost::compressed_pair uses a technique known as empty base class optimization.

#include <boost/compressed_pair.hpp>
#include <utility>
#include <iostream>

struct empty [};

int main() {
  std::pair<int, empty> p;
  std::cout << sizeof(p) << std::endl;

  boost::compressed_pair<int, empty> cp;
  std::cout << sizeof(cp) << std::endl;

  return 0;
}

输出为:

8

4

There is another difference between boost::compressed_pair and std::pair. the values stored in boost::compressed_pair are accessed through the member functions first() and second(), std::pair uses two indentically named member variables instead.

posted @ 2019-07-02 19:34  c++11  阅读(372)  评论(0编辑  收藏  举报