其他分享
首页 > 其他分享> > boost compressedPair

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.

标签:std,compressedPair,compressed,pair,include,boost,empty
来源: https://www.cnblogs.com/sssblog/p/11122748.html