Using std::pair

Using std::pair with References

Can I store references in a std::pair, and what are the implications?

Abstract art representing computer programming

Yes, you can store references in a std::pair. When you do so, the pair will hold references to the original objects, rather than storing copies of the objects themselves. This can be useful in certain situations, but it also comes with some implications and considerations.  Example:

#include <iostream>
#include <utility>

int main() {
  int a = 42;
  const std::string b = "Hello";

  std::pair<int&, const std::string&> myPair(a, b);  

  std::cout << "a: " << myPair.first
    << ", b: " << myPair.second << '\n';

  a = 10;  
  std::cout << "a: " << myPair.first
    << ", b: " << myPair.second << '\n';
}
a: 42, b: Hello
a: 10, b: Hello

In this example, myPair is declared as a std::pair that holds references to an int and a const std::string. When we initialize myPair with a and b, the pair stores references to the original variables.

When we modify a later in the code, the change is reflected in myPair.first because it refers to the same int object.

Implications and considerations:

  1. Lifetime: When storing references in a pair, you must ensure that the referenced objects outlive the pair itself. If the referenced objects are destroyed before the pair, accessing the references in the pair will lead to undefined behavior.
  2. Const-correctness: If you want to store a reference to a const object, you need to use const in the pair's type declaration, as shown in the example with const std::string&.
  3. Assignment: When assigning a new value to a pair element that is a reference, it modifies the referenced object, not the reference itself.
  4. Initialization: If you initialize a pair with references using the default constructor, the references will be uninitialized and accessing them will result in undefined behavior. Always ensure that references in a pair are properly initialized.
  5. Moved-from state: If a pair holding references is moved from, the references in the moved-from pair will still refer to the original objects. The moved-from pair is in a valid but unspecified state.

Use references in a std::pair judiciously and ensure that you handle the lifetime and const-correctness correctly to avoid issues.

This Question is from the Lesson:

Using std::pair

Master the use of std::pair with this comprehensive guide, encompassing everything from simple pair manipulation to template-based applications

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Using std::pair

Master the use of std::pair with this comprehensive guide, encompassing everything from simple pair manipulation to template-based applications

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved