What is std::pair?

前端 未结 10 1724
面向向阳花
面向向阳花 2020-12-12 19:07

What is std::pair for, why would I use it, and what benefits does boost::compressed_pair bring?

相关标签:
10条回答
  • 2020-12-12 19:44

    It can sound strange to hear that compressed_pair cares about a couple of bytes. But it can actually be important when one considers where compressed_pair can be used. For example let's consider this code:

    boost::function<void(int)> f(boost::bind(&f, _1));
    

    It can suddenly have a big impact to use compressed_pair in cases like above. What could happen if boost::bind stores the function pointer and the place-holder _1 as members in itself or in a std::pair in itself? Well, it could bloat up to sizeof(&f) + sizeof(_1). Assuming a function pointer has 8 bytes (not uncommon especially for member functions) and the placeholder has one byte (see Logan's answer for why), then we could have needed 9 bytes for the bind object. Because of aligning, this could bloat up to 12 bytes on a usual 32bit system.

    boost::function encourages its implementations to apply a small object optimization. That means that for small functors, a small buffer directly embedded in the boost::function object is used to store the functor. For larger functors, the heap would have to be used by using operator new to get memory. Around boost version 1.34, it was decided to adopt this optimization, because it was figured one could gain some very great performance benefits.

    Now, a reasonable (yet, maybe still quite small) limit for such a small buffer would be 8 bytes. That is, our quite simple bind object would not fit into the small buffer, and would require operator new to be stored. If the bind object above would use a compressed_pair, it can actually reduce its size to 8 bytes (or 4 bytes for non-member function pointer often), because the placeholder is nothing more than an empty object.

    So, what may look like just wasting a lot of thought for just only a few bytes actually can have a significant impact on performance.

    0 讨论(0)
  • 2020-12-12 19:46

    You sometimes need to return 2 values from a function, and it's often overkill to go and create a class just for that.

    std:pair comes in handy in those cases.

    I think boost:compressed_pair is able to optimize away the members of size 0. Which is mostly useful for heavy template machinery in libraries.

    If you do control the types directly, it's irrelevant.

    0 讨论(0)
  • 2020-12-12 19:46

    Sometimes there are two pieces of information that you just always pass around together, whether as a parameter, or a return value, or whatever. Sure, you could write your own object, but if it's just two small primitives or similar, sometimes a pair seems just fine.

    0 讨论(0)
  • 2020-12-12 19:54

    std::pair is a data type for grouping two values together as a single object. std::map uses it for key, value pairs.

    While you're learning pair, you might check out tuple. It's like pair but for grouping an arbitrary number of values. tuple is part of TR1 and many compilers already include it with their Standard Library implementations.

    Also, checkout Chapter 1, "Tuples," of the book The C++ Standard Library Extensions: A Tutorial and Reference by Pete Becker, ISBN-13: 9780321412997, for a thorough explanation.

    alt text

    0 讨论(0)
  • 2020-12-12 19:57

    It's nothing but a structure with two variables under the hood.

    I actually dislike using std::pair for function returns. The reader of the code would have to know what .first is and what .second is.

    The compromise I use sometimes is to immediately create constant references to .first and .second, while naming the references clearly.

    0 讨论(0)
  • 2020-12-12 20:05

    compressed_pair uses some template trickery to save space. In C++, an object (small o) can not have the same address as a different object.

    So even if you have

    struct A { };
    

    A's size will not be 0, because then:

    A a1;
    A a2;
    &a1 == &a2;
    

    would hold, which is not allowed.

    But many compilers will do what is called the "empty base class optimization":

    struct A { };
    struct B { int x; };
    struct C : public A { int x; };
    

    Here, it is fine for B and C to have the same size, even if sizeof(A) can't be zero.

    So boost::compressed_pair takes advantage of this optimization and will, where possible, inherit from one or the other of the types in the pair if it is empty.

    So a std::pair might look like (I've elided a good deal, ctors etc.):

    template<typename FirstType, typename SecondType>
    struct pair {
       FirstType first;
       SecondType second;
    };
    

    That means if either FirstType or SecondType is A, your pair<A, int> has to be bigger than sizeof(int).

    But if you use compressed_pair, its generated code will look akin to:

     struct compressed_pair<A,int> : private A {
        int second_;
        A first() { return *this; }
        int second() { return second_; }
     };
    

    And compressed_pair<A,int> will only be as big as sizeof(int).

    0 讨论(0)
提交回复
热议问题