A draft of the proposal can be found here: http://acmorrow.github.com/swap_traits/nXXXX.html
But,
like the imaginary code above, I think the problem is on
ADL, not swap itself.
For short, I want a `using` directive for a function, only, like
a function level `try` block.
void f(T& x, T& y) using std::swap noexcept(noexcept(swap(x, y)))
5.2.2013 23:22, Nikolay Ivchenkov kirjoitti:
> 1. IMO, the expression should be swap(declval<T>(), declval<U>())
> rather than swap(declval<T&>(), declval<U&>()), so we could test
> lvalues and rvalues.
>
So that in the common case you need to use is_swappable<A&> ?
2. The wording "The set of swaps considered for resolution must include std::swap as well as swap overloads available via argument dependent lookup" isn't precise enough, at least because there are several templates with name 'swap' in std.
3. An implementation of is_swappable and is_nothrow_swappable could be more simple:
On Wednesday, February 6, 2013 1:51:57 PM UTC+4, Mikael Kilpeläinen wrote:5.2.2013 23:22, Nikolay Ivchenkov kirjoitti:
> 1. IMO, the expression should be swap(declval<T>(), declval<U>())
> rather than swap(declval<T&>(), declval<U&>()), so we could test
> lvalues and rvalues.
>
So that in the common case you need to use is_swappable<A&> ?
I don't know what common case you have in mind. I can imagine an application like this:
is_swappable<decltype(*declval<dereferenceable_type>())>
where the result of the dereferencing could be a prvalue of a proxy type.
which would require is_nothrow_swappable<T&> to pick up swap(T&, T&).
First i want to say that i am sorry, I got your respond now.. gotta love gmail.
I don't see problems with is_nothrow_swappable<T&>.
Well, cases where left and right arguments for swap have different types are also rare, but the proposal tends to support such rare applications, right?
I don't think it is valid argument to find something as surprising in the standard.. yes, we have many surprising things, we ought to minimise those. But like said earlier,
Is the definition of std::is_assignable surprising? How often do we use rvalues as the left operand of assignment operator? Wouldn't different conventions about interpretation of template arguments for std::is_assignable and std::is_swappable be surprising for people?
I have to say more I think about it, the more I am turning to want what you said.
Mostly because what does swappable type mean? Is the lvalue of it swappable or rvalue or something else?
Indeed, and that is the point. Your way seems to make least confusion, at least after little thought.. as it applies to the exact "expression" you are giving.
There may be different definitions.
One option would be to have special case for the plain type..
Yes, that is along the lines i was suggesting...
Do you mean something like this?
// general version
template <class T, class U = T>
struct is_swappable_with;
template <class T>
struct is_swappable :
is_swappable_with
<
typename add_lvalue_reference<T>::type,
typename add_lvalue_reference<T>::type
> {};
I think, it would be acceptable.
Btw, 17.6.3.2/2 has the symmetry contraint for swappable .. swap(t, u) and swap(u, t) must be valid. Should we consider this as well?
Perhaps.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.
- Nikolay's comment on wording: I agree the wording is not sufficiently precise, but I'm not familiar enough with the idioms of the standard to do much better on my own so I did not attempt to perfect it. Suggestions on how to improve wording anywhere it is weak in the proposal would be much appreciated.
- Nikolay's alternative implementation: I like the compactness of it,
but the required changes to std::swap makes it less appealing to me.
--
Based on the feedback and discussion in this thread, I have updated my proposal for the addition of std::is_swappable and std::is_nothrow_swappable as follows:
- Changed from declval<T&> to declval<T>, and for U.- Added the swap(t,u) swap(u, t) symmetry requirement for is_swappable.- Switched from "type relationship" to "type properties"- Proposed additions are now to Table 49, rather than table 51.- Adopted Nikolay's proposed wording for table 49.- Updated the example implementation as if it were actually being written in 'namespace std' for a standard library header implementation.
The updated draft can be found here: http://acmorrow.github.com/swap_traits/nXXXX.html
Looking good, I will take closer look when I have time but small remark now..I assume this means one has to explicitly include <utility> when ever these traits are used.
"The context in which the aforementioned expressions are considered shall ensure that a candidate set for swap consists of the two swap function templates defined in <utility> (20.2) and the lookup set produced by argument-dependent lookup (3.4.2)."
Then the wording seems bit vague though, how do I ensure those swap functions will be candidates if I don't know how it is implemented?
I would rather see something like "if the header <utility> is not included prior to a use of these traits the program is ill-formed".
Another option would be to specify that the new header includes <utility> which i think is fine, in which case that needs to be added to the synopsis.
Mikael
I think I disagree. I don't see anything in 17.6.3.2 that requires two swap implementations forming a heterogeneous swap pair to share a noexcept status:
class Foo {};class Bar {};
void swap(Foo& f, Bar& b) noexcept {// ...}
void swap(Bar& b, Foo& f) {// ...}
So is_swappable<Foo, Bar>::value and is_swappable<Bar, Foo>::value should clearly both be true_type, along with is_nothrow_swappable<Foo, Bar>::value, but I think is_nothrow_swappable<Bar, Foo>::value should still be false_type.
"An object t is swappable with an object u if and only if:
— the expressions swap(t, u) and swap(u, t) are valid when evaluated in the context described below,
and
— these expressions have the following effects:
— the object referred to by t has the value originally held by u and
— the object referred to by u has the value originally held by t."
Now if I throw exception the last two sentences are not true any more. So one could even read this that no exceptions can be thrown which seems overly tight
and was not meant most likely. Am I missing something here?
That is an interesting point: it does seem like the language here forbids throwing swaps, but I think that must be unintentional, or that we are misreading it, or that another section provides an escape. See 20.2.2, which defines the noexcept status of the default std::swap template, which would be pointless if swap were meant to be forbidden from throwing. So perhaps the language in 17.6.3.2 needs to be clarified, but that is independent of my proposal. I suppose if it were determined that throwing swaps were in fact forbidden that is_nothrow_swappable would become useless.
�
Would having is_value_swappable make sense for convenience (per 17.6.3.2/5)?
Perhaps, but I think this proposal as written is close to complete (and thank you for your help getting it there), so I'm not sure I want to open another front. I saw in another post that the deadline for submission to the LWG chair for the next mailing is this week, and I'd like to make that deadline if possible. Do you think the current draft is sufficiently polished to submit?
I agree. I think the draft is good enough to be submitted. The deadline is friday.
I still don't see any useful applications of such is_swappable. It doesn't answer whether the provided argument(s) is/are swappable, even roughly. A call to the general unconstrained version of std::swap, where both arguments are lvalues of the same type, will be always well-formed. If you don't want to change the declaration of std::swap, you can introduce some constrained surrogate for it and construct the candidate set from the surrogate and versions found by ADL:
--