Swap function overloads for bit proxy classes

46 views
Skip to first unread message

Vincent Reverdy

unread,
Jan 25, 2016, 4:12:55 PM1/25/16
to ISO C++ Standard - Discussion
Hello.

I am wondering how std::swap should be overloaded for the proposal I describe here about bit utilities.
In the following, a bit_value models an independent, non-referenced bit, whereas bit_reference<UIntType> mimics a reference to a bit within an integer.
I am wondering, which overloads of std::swap should be provided for these classes, as they all refer to the same concept of a bit.
  1. std::swap(bit_value& lhs, bit_value& rhs); // Would have the name behaviour as the generic templated std::swap
  2. template <class T> std::swap(bit_reference<T> lhs, bit_reference<T> rhs);
  3. template <class T> std::swap(bit_reference<T>& lhs, bit_reference<T>& rhs);
  4. template <class T> std::swap(bit_reference<T>&& lhs, bit_reference<T>&& rhs);
  5. template <class T> std::swap(bit_reference<T> lhs, bit_value& rhs);
  6. template <class T> std::swap(bit_reference<T>& lhs, bit_value& rhs);
  7. template <class T> std::swap(bit_reference<T>&& lhs, bit_value& rhs);
  8. template <class T> std::swap(bit_value& lhs, bit_reference<T> rhs);
  9. template <class T> std::swap(bit_value& lhs, bit_reference<T>& rhs);
  10. template <class T> std::swap(bit_value& lhs, bit_reference<T>&& rhs);
  11. template <class T, class U> std::swap(bit_reference<T> lhs, bit_reference<U> rhs);
  12. template <class T, class U> std::swap(bit_reference<T> lhs, bit_reference<U>& rhs);
  13. template <class T, class U> std::swap(bit_reference<T> lhs, bit_reference<U>&& rhs);
  14. template <class T, class U> std::swap(bit_reference<T>& lhs, bit_reference<U>& rhs);
  15. template <class T, class U> std::swap(bit_reference<T>& lhs, bit_reference<U>&& rhs);
  16. template <class T, class U> std::swap(bit_reference<T>&& lhs, bit_reference<U>&& rhs);

Does the standard provide some guidance over what should be provided, and what should not, and based on what criterion?


Thanks,

Vincent


Howard Hinnant

unread,
Jan 25, 2016, 5:03:26 PM1/25/16
to std-dis...@isocpp.org
The standard doesn’t provide much guidance here. Assuming that bit_reference<T> is a light-weight object with reference semantics, I recommend passing it by value to swap. This is what libc++ does as an extension:

https://github.com/llvm-mirror/libcxx/blob/master/include/__bit_reference#L84-L122

libc++ does not have a bit_value, but does get the combinations with bool&. Overloading any type on by-value, by-lvalue_reference and by-rvalue_reference is generally not a good idea. *If* bit_reference<T> is expensive to copy (it shouldn’t be), then one might consider overloading on bit_reference<T>& and bit_reference<T>&&.

Howard

Vincent Reverdy

unread,
Jan 26, 2016, 2:34:28 PM1/26/16
to ISO C++ Standard - Discussion
Yes, after some benchmarks (3 platforms, both with g++ and clang) to check, passing it by value in my implementation (bit_reference contains a pointer and a mask) does not harm at all, (except in -O1 in g++).
So, I guess passing it by value + the bit_value mixed version will be enough.
Reply all
Reply to author
Forward
0 new messages