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
?