std::pair<reference,reference>

Theodore Papadopoulo Theodore.Papadopoulo@sophia.inria.fr
Thu May 18 10:08:00 GMT 2006


On Thu, 2006-05-18 at 00:21 -0500, Benjamin Kosnik wrote:
> > I wonder whether it would be possible to add some specializations of
> > pair for it to work even one of its template arguments is a reference.
> > Below is the specialization example for the case  when the two
> > parameters are references. Obviously, DR 106 (if/when
> > adopted/implemented) is a better fix, but in the meantime, maybe a awful
> > trick like this one could be considered as a quality of
> > implementation....
> 
> Hmm. Are you sure you have the right number? 

I'm not sure I understand. You mean about DR 106. It looked like it but
I may be wrong...
It's about the langage forbidding to take references of references.

> include <utility>
> 
> int main()
> {
>  using namespace std;
> 
>  int i = 6;
>  int j = 7;
>  int& r1 = i;
>  int& r2 = j;
>  make_pair(r1, r2);
>  pair<r1, r2> p;
> }
> 
> %g++ -c pair.cc
> pair.cc: In function 'int main()':
> pair.cc:12: error: 'r1' cannot appear in a constant-expression
> pair.cc:12: error: 'r2' cannot appear in a constant-expression
> pair.cc:12: error: template argument 1 is invalid
> pair.cc:12: error: template argument 2 is invalid
> pair.cc:12: error: invalid type in declaration before ';' token
> 
> This, you mean?

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);
}

samoa-> g++ toto.c
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c+
+/3.4.4/bits/stl_pair.h: In instantiation of `std::pair<int&, int&>':
toto.c:11:   instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c+
+/3.4.4/bits/stl_pair.h:85: error: forming reference to reference type
`int&'
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c+
+/3.4.4/bits/stl_pair.h: In constructor `std::pair<_T1, _T2>::pair(const
std::pair<_U1, _U2>&) [with _U1 = int, _U2 = int, _T1 = int&, _T2 =
int&]':
toto.c:11:   instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c+
+/3.4.4/bits/stl_pair.h:90: error: invalid initialization of reference
of type 'int&' from expression of type 'const int'
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c+
+/3.4.4/bits/stl_pair.h:90: error: invalid initialization of reference
of type 'int&' from expression of type 'const int'

> > If acceptable, I would be glad to implement and test it.
> Sure.

I'll do that...




More information about the Libstdc++ mailing list