Topic: reference conversion operator query


Author: David Vandevoorde <daveed@vandevoorde.com>
Date: 1997/06/04
Raw View
Brian Parker wrote:
>
> I have a query concerning the binding of references to lvalues.
[...]
> By my reading of the CD2, // 2 is allowed as the argument is an int
> lvalue expression. I previously though that // 1 was explicitly
> allowed by 8.5.3 [dcl.init.ref] paragraph 6, but, on rereading, I
> think it is disallowed as the Proxy class is an rvalue.
> Is this a correct interpretation?

This exact topic was the subject of a paper at the Nashua
meeting (triggered by almost exactly the example you show
used in a prototype implementation of valarray).

I believe this was made part of the U.S.A.'s national body
comments (which gives it a pretty good chance to be
incorporated in the WP in July; if I understand the
procedures correctly).

FWIW, the paper also noted that all compilers tried actually
do what we both want (even older and/or notoriously non-
standard compilers ;-). It continues to treat the case
of const references (which currently require an extra
copy to be bound to).

 Daveed
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: bparker@gil.com.au (Brian Parker)
Date: 1997/06/07
Raw View
On 04 Jun 1997 10:49:04 PDT, David Vandevoorde
<daveed@vandevoorde.com> wrote:
>...
>This exact topic was the subject of a paper at the Nashua
>meeting (triggered by almost exactly the example you show
>used in a prototype implementation of valarray).
>
>I believe this was made part of the U.S.A.'s national body
>comments (which gives it a pretty good chance to be
>incorporated in the WP in July; if I understand the
>procedures correctly).

That's good news. There are just two subtleties I hope the committee
considers though-
(1) Such proxy classes are often used to distinguish lvalue/rvalue
usage of an array, and hence one would need to add both-
operator int( ) const; // used for rvalue usage
operator int&( );  // used to initialise non-const
   // references (i.e. lvalue usage)

to the proxy class. I am not sure, however, if the overloading rules
are sufficient to ensure the behaviour specified in the comments
above, e.g. for

void func(const int& i);
func(array[i]);  // array[i] returns proxy

is the call ambiguous? (I would hope that operator int( ) was called
as this is rvalue usage).

If this common usage is ambiguous (it may not be), then I would like
to see a note added to the draft limiting the implicit calling of
operator T&() to *only* the initialisation of non-const references.

(2) I think that one should be able to return *this from operator
T&(). This would allow a class designer to allow temporaries of his
class (even those with value semantics) to be used in calls to
functions taking non-const references (although this could admittedly
be misused as it would lead to a dangling reference if used to
initialise a stand-alone reference).  Section 12.3.2 [class.conv.fct]
paragraph 1, however, currently disallows this usage.

,Brian Parker (bparker@gil.com.au)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: bparker@gil.com.au (Brian Parker)
Date: 1997/06/02
Raw View
I have a query concerning the binding of references to lvalues.

In the following code, are the lines with comments // 1 and // 2
legal?

// proxy for an int &
class Proxy {
public:
 Proxy(int& n) : data_(n) {}
 operator int&() {return data_;}

 int& data_;
};

// simple array class
class C {
public:
 // init to 0
 C( ) {for(int i=0; i < 10; i++) data[i] = 0;}

 // indexing
 Proxy operator[](int n) {return Proxy(data[n]);}

 int data[10];
};

void foo(int& n)
{
 n = 1;
}

int main()
{
 C vec;

 foo(vec[2]); // 1

 foo(static_cast<int&>(vec[2])); // 2

 return 0;
}

By my reading of the CD2, // 2 is allowed as the argument is an int
lvalue expression. I previously though that // 1 was explicitly
allowed by 8.5.3 [dcl.init.ref] paragraph 6, but, on rereading, I
think it is disallowed as the Proxy class is an rvalue.
Is this a correct interpretation?

If so, then IHMO it would be extremely useful if paragraph 6 was
modified so that // 1 was also legal to better support the use of such
temporary reference-semantics classes.

,Brian Parker (bparker@gil.com.au)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]