This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: std::pair<reference,reference>


On May 18, 2006, at 12:38 PM, Benjamin Kosnik wrote:


No I mean this:

#include <utility>

int main()
{
 using namespace std;

 int i = 6;
 int j = 7;
 int& r1 = i;
 int& r2 = j;
 pair<int&, int&> p = make_pair(r1, r2);
}

Sure, your initial suggestion seems like a winner.

You might take a look at how tuple is done (which already has this functionality).


      pair(const _T1& __a, const _T2& __b)
      : first(__a), second(__b) { }


Besides the reference-to-reference problem this will allow you to bind a non-const reference to a temporary (which is bad).


In a nutshell, if you've got a reference type, you need to accept it by value in an argument list:

template <class T>
struct param
{
    typedef const T& type;
};

template <class T>
struct param<T&>
{
    typedef T& type;
};

template <class T1, class T2>
struct pair
{
    pair(typename param<T1>::type a, ...);
};

pair<int&, int&> p(1, 2);  // compile time error (which is good)
int i;
pair<int&, int&> p(i, i);  // ok

Also:

typedef _T1 first_type; ///< @c first_type is the first bound

should be:


typedef _T1& first_type;

You don't want to tell people you've got an int when you really have an int&.

-Howard


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]