3fdef3c8   
   From: daniel.kruegler@googlemail.com   
      
   On 2011-05-20 00:41, Joshua Lehrer wrote:   
   >   
   > On May 18, 4:49 pm, Kai-Uwe Bux wrote:   
   >> Joshua Lehrer wrote:   
   >>> I was challenged to come up with a way to use existing STL algorithms   
   >>> to sort one vector but given sorting precedence in a separate vector.   
   >>> An additional constraint is that only O(1) constant external storage   
   >>> may be used.   
   >>   
   >> to be modified and the first should stay the same. The following does sort   
   >> _both_ vectors based upon the comparison from the first. It is faking the   
   >> reference that you need as the return type for operator*.   
   >   
   > Kai-Uwe, your solution is the closest, and I went this route as well.   
   > However, there is still a problem. Operator* must return a true   
   > reference, you can not return a temporary object.   
   >   
   > swap(*a,*b)   
   >   
   > is the problem. swap takes two non-const references. If *a returns a   
   > temporary, not a true reference, then it can't bind this temporary to   
   > a non-const reference, and it fails to compile.   
      
   I agree that the random access iterator requirements basically impose   
   the requirement that the expression *it (given an iterator value named   
   'it') is a real reference. This is a recognized design defect in the   
   iterator categories, because traversal and access are not controlled by   
   independent axes. Note that C++0x has introduced swappable requirements   
   and these also support proxy references because given these swappable   
   requirements an implementation has to ensure that swap calls are   
   "ADL-enabled" and there is no requirement (in general) that such swap   
   calls must accept lvalues. It was explicit part of the design of these   
   requirements to allow user-swap functions to accept rvalues and to allow   
   mixed swaps as well. This leads to the grey zone, where the a strict   
   interpretation of the random access iterator requirements meets with the   
   more general swap requirements. In this case I would expect that a   
   modern C++0x-enabled library implementation just accepts random access   
   iterator types, where the expression *it does not return a real   
   reference, because the design problem of iterators is well-known and   
   will hopefully be fixed in the future.   
      
   > I ran into this problem as well trying to implement it myself:   
   >   
   >> I ran into trouble when I had to implement operator* on my pointer class   
   because I had   
   >> no real object to return a reference to   
   >   
   > This shouldn't be so hard, but I continue to claim that it is not   
   > possible with std::sort.   
      
   It should be possible in a C++0x-enabled library implementation,   
   especially since C++0x sort is required to respect the Swappable   
   requirements on the *caller* of swap (i.e. to be enforced to activate ADL).   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|