Title
The CommonReference requirement of concept SwappableWith is not satisfied in the example
Status
new
Section
[concept.swappable]
Submitter
Kostas Kyrimis

Created on 2018-12-14.00:00:00, last changed 2018-12-15.21:32:24.

Messages

Date: 2018-12-17.17:04:00

Proposed resolution:

This wording is relative to N4791.

  1. Change [concept.swappable] as indicated:

    -3- […]

    template<class T>
      concept Swappable = requires(T& a, T& b) { ranges::swap(a, b); };
      
    template<class T, class U>
      concept SwappableWith =
      CommonReference<T, Uconst remove_reference_t<T>&, const remove_reference_t<U>&> &&
      requires(T&& t, U&& u) {
        ranges::swap(std::forward<T>(t), std::forward<T>(t));
        ranges::swap(std::forward<U>(u), std::forward<U>(u));
        ranges::swap(std::forward<T>(t), std::forward<U>(u));
        ranges::swap(std::forward<U>(u), std::forward<T>(t));
      };
    

    -4- […]

    -5- [Example: User code can ensure that the evaluation of swap calls is performed in an appropriate context under the various conditions as follows:

    #include <cassert>
    #include <concepts>
    #include <utility>
    
    namespace ranges = std::ranges;
    
    template<class T, std::SwappableWith<T> U>
    void value_swap(T&& t, U&& u) {
      ranges::swap(std::forward<T>(t), std::forward<U>(u));
    }
    
    template<std::Swappable T>
    void lv_swap(T& t1, T& t2) {
      ranges::swap(t1, t2);
    }
    
    namespace N {
      struct A { int m; };
      struct Proxy { 
        A* a;
        Proxy(A& a) : a{&a} {}
        friend void swap(Proxy&& x, Proxy&& y) {
          ranges::swap(x.a, y.a);
        }
      };
      Proxy proxy(A& a) { return Proxy{ &a }; }
      void swap(A& x, Proxy p) {
        ranges::swap(x.m, p.a->m);
      }
      void swap(Proxy p, A& x) { swap(x, p); } // satisfy symmetry requirement
    }
    
    int main() {
      int i = 1, j = 2;
      lv_swap(i, j);
      assert(i == 2 && j == 1);
      N::A a1 = { 5 }, a2 = { -5 };
      value_swap(a1, proxy(a2));
      assert(a1.m == -5 && a2.m == 5);
    }
    
Date: 2018-12-17.17:04:00

The defect stems from the example found in sub-clause [concept.swappable] p5:

[…]

template<class T, std::SwappableWith<T> U>
void value_swap(T&& t, U&& u) {
  ranges::swap(std::forward<T>(t), std::forward<U>(u));
}

[…]
namespace N {
  struct A { int m; };
  struct Proxy { A* a; };
  Proxy proxy(A& a) { return Proxy{ &a }; }
  
  void swap(A& x, Proxy p) {
    ranges::swap(x.m, p.a->m);
  }
  void swap(Proxy p, A& x) { swap(x, p); } // satisfy symmetry requirement
}

int main() {
  […]
  N::A a1 = { 5 }, a2 = { -5 };
  value_swap(a1, proxy(a2)); // diagnostic manifests here(#1)
  assert(a1.m == -5 && a2.m == 5);
}

The call to value_swap(a1, proxy(a2)) resolves to [T = N::A&, U = N::Proxy] The compiler will issue a diagnostic for #1 because:

  1. rvalue proxy(a2) is not swappable

  2. concept SwappableWith<T, U> requires N::A and Proxy to model CommonReference<const remove_reference_t<T>&, const remove_reference_t<U>&> It follows from the example that there is no common reference for [T = N::A&, U = N::Proxy]

History
Date User Action Args
2018-12-15 21:32:24adminsetmessages: + msg10256
2018-12-14 00:00:00admincreate