0

So I am looking through this handout that describes the code for std::pair. Below is the code:

template <class U, class V>
struct pair {
    U first;
    V second;
    pair(const U& first = U(), const V& second = V()) :
        first(first), second(second) {}
};
template <class U, class Y>
pair<U, V> make_pair(const U& first, const V& second);

I am trying to understand this code but I am having problems, specifically at the line pair in the struct. I understand that we store two create two variables first and second according to the respective classes.

In the argument of the pair function, I see that we create a new class U and V and assign them respectively to first and second, but I don't clearly understand how the const U& works because of the ampersand sign. What's more confusing is the use of a colon after the function declaration which I have never seen before used in c++.

I also don't understand the line below declaring first(first) and second(second) especially with the brackets. Isn't first a type, so how are we able to call a function from first?

Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
ruthless
  • 1,090
  • 4
  • 16
  • 36
  • 9
    [References](http://en.cppreference.com/w/cpp/language/reference) and [constructor initializer lists](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor). – chris Aug 13 '14 at 16:34
  • 1
    The constructor `pair(const U& first = U(), const V& second = V())` may look a bit dubious, but remember that you can bind temporary objects to `const` references. What this does in effect is initializing by default the members `first` and `second` with the default values (0 for plain old types), in case you do not specify the arguments. `pair` uses pass-by-const reference to avoid copying of large objects. – vsoftco Aug 13 '14 at 16:43
  • No, `first` is not a type, it is a member of type `U`. – Michael Dorst Aug 13 '14 at 17:01
  • For future reference, when asking multiple questions, ask each one in it's own post. – Michael Dorst Aug 13 '14 at 17:10

1 Answers1

2

We'll address this by dividing it into parts.

U& means that we are passing a variable of type U that will be used by reference - the variable used by the constructor is the same one (same memory address and value) that is given as an argument. By saying const U& first = U() we are saying that we promise not to change the first passed into the constructor (const ...), we want first to be taken by reference (...U&...), and if we don't provide first in the constructor we should use a U provided by U's default constructor (... = U()). For more information on references, this page should help.

first(first) is part of a "constructor initialization list" - the preferred method for initializing class member variables in a constructor. We tell the constructor that we are initializing the member variable of pair called first (first(...)) with the argument first provided by the constructor (the U& we discussed earlier). For more information on constructor initialization lists, this page should help.

Conduit
  • 2,675
  • 1
  • 26
  • 39
  • Related: both of these concepts are VERY important! Make sure you learn them thoroughly; your code will be much better for it. – Conduit Aug 13 '14 at 17:40