Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7591753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:43:45+00:00 2026-05-30T20:43:45+00:00

I work a lot with pairs of values: std::pair<int, int> my_pair . Sometimes I

  • 0

I work a lot with pairs of values: std::pair<int, int> my_pair. Sometimes I need to perform the same operation on both my_pair.first and my_pair.second.

My code would be much smoother if I could do my_pair[j] and loop over j=0,1.
(I am avoiding using arrays because I don’t want to bother with allocating memory, and I use pair extensively with other things).

Thus, I would like to define operator[] for std::pair<int, int>.

And I can’t get it to work, (I’m not very good with templates and such)…

#include <utility>
#include <stdlib.h>

template <class T1> T1& std::pair<T1, T1>::operator[](const uint &indx) const
{
  if (indx == 0)
    return first;
  else
    return second;
};

int main()
{
// ....
return 0;
}

fails to compile. Other variations fail as well.

As far as I can tell, I am following the Stack Overflow operator overloading FAQ, but I guess I am missing something…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:43:46+00:00Added an answer on May 30, 2026 at 8:43 pm
    1. you cannot overload operator[] as a non-member
    2. you cannot define a member function which has not been declared in the class definition
    3. you cannot modify the class definition of std::pair

    Here’s a non-member implementation:

    /// @return the nth element in the pair. n must be 0 or 1.
    template <class T>
    const T& pair_at(const std::pair<T, T>& p, unsigned int n)
    {
        assert(n == 0 || n == 1 && "Pair index must be 0 or 1!");
        return n == 0 ? p.first: p.second;
    }
    
    /// @return the nth element in the pair. n must be 0 or 1.
    template <class T>
    T& pair_at(std::pair<T, T>& p, unsigned int index)
    {
        assert(index == 0 || index == 1 && "Pair index must be 0 or 1!");
        return index == 0 ? p.first: p.second;
    }
    
    // usage:
    pair<int, int> my_pair(1, 2);
    for (int j=0; j < 2; ++j)
        ++pair_at(my_pair, j);
    

    Note that we need two versions: one for read-only pairs and one for mutable pairs.

    Don’t be afraid to use non-member functions liberally. As Stroustrup himself said, there is no need to model everything with an object or augment everything through inheritance. If you do want to use classes, prefer composition to inheritance.

    You can also do something like this:

    /// Applies func to p.first and p.second.
    template <class T, class Func>
    void for_each_pair(const std::pair<T, T>& p, Func func)
    {
        func(p.first);
        func(p.second);
    }
    
    /// Applies func to p.first and p.second.
    template <class T, class Func>
    void for_each_pair(std::pair<T, T>& p, Func func)
    {
        func(p.first);
        func(p.second);
    }
    
    // usage:
    pair<int, int> my_pair(1, 2);
    for_each_pair(my_pair, [&](int& x){
        ++x;
    });
    

    That isn’t too unwieldy to use if you have C++11 lambdas and is at least a bit safer since it has no potential to access out of bounds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I work a lot with 3rd party web services. The first thing I look
in my work we do a lot of pair programming, and i wrote a
We work with a lot of real estate, and while rearchitecting how the data
I do a lot of work with temperatures in Textmate and I'd love to
I've been doing a lot of work with tuples and lists of tuples recently
I have an MVC app that does a lot of work with jQuery ajax
My work is running SQL Server 2008 and I spend a lot of time
Having a lot of trouble getting texture maps to work in openGL ES (iphone).
I used to work in JavaScript a lot and one thing that really bothered
The company I work for produces a lot of video and we want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.