220 2702 <510E1906.3030604@wanadoo.fr> article
Path: news.gmane.org!not-for-mail
From: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: A proposal to add swap traits to the standard library
Date: Sun, 03 Feb 2013 09:00:06 +0100
Lines: 178
Approved: news@gmane.org
Message-ID: <510E1906.3030604@wanadoo.fr>
References: <CA+Acj4fnCPDP2wPVts1B4Zaz6xzZ899=A=6ShTUqw_Ep5gN7pg@mail.gmail.com> <1359827596.1381.2.camel@Nokia-N900> <41C25E3D-C254-43AE-BFEA-ED83B7DAF74E@gmail.com> <CANh-dXkVgtEtkUvzCkS9Gia_Rk_iesULRcH3oC-OyqX-Z7pPNA@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding: quoted-printable
X-Trace: ger.gmane.org 1359878408 4502 80.91.229.3 (3 Feb 2013 08:00:08 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Sun, 3 Feb 2013 08:00:08 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDH67CONY4PBBB5SXCEAKGQE6C6QLXI@isocpp.org Sun Feb 03 09:00:28 2013
Return-path: <std-proposals+bncBDH67CONY4PBBB5SXCEAKGQE6C6QLXI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-lb0-f200.google.com ([209.85.217.200])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDH67CONY4PBBB5SXCEAKGQE6C6QLXI@isocpp.org>)
	id 1U1uV0-0000lZ-1y
	for gclcip-std-proposals@m.gmane.org; Sun, 03 Feb 2013 09:00:26 +0100
Original-Received: by mail-lb0-f200.google.com with SMTP id k6sf6092672lbo.3
        for <gclcip-std-proposals@m.gmane.org>; Sun, 03 Feb 2013 00:00:07 -0800 (PST)
X-Received: by 10.180.74.141 with SMTP id t13mr856902wiv.5.1359878407420;
        Sun, 03 Feb 2013 00:00:07 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.180.78.34 with SMTP id y2ls424635wiw.14.canary; Sun, 03 Feb
 2013 00:00:07 -0800 (PST)
X-Received: by 10.180.87.170 with SMTP id az10mr4754051wib.3.1359878406988;
        Sun, 03 Feb 2013 00:00:06 -0800 (PST)
X-Received: by 10.180.87.170 with SMTP id az10mr4754049wib.3.1359878406968;
        Sun, 03 Feb 2013 00:00:06 -0800 (PST)
Original-Received: from smtp.smtpout.orange.fr (smtp03.smtpout.orange.fr. [80.12.242.125])
        by mx.google.com with ESMTP id r9si2289312wiv.76.2013.02.03.00.00.06;
        Sun, 03 Feb 2013 00:00:06 -0800 (PST)
Received-SPF: neutral (google.com: 80.12.242.125 is neither permitted nor denied by best guess record for domain of vicente.botet@wanadoo.fr) client-ip=80.12.242.125;
Original-Received: from iMac-de-Vicente-Botet-Escriba.local ([2.11.186.153])
	by mwinf5d57 with ME
	id vk061k0043K0JVN03k06Vq; Sun, 03 Feb 2013 09:00:06 +0100
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:17.0) Gecko/20130107 Thunderbird/17.0.2
In-Reply-To: <CANh-dXkVgtEtkUvzCkS9Gia_Rk_iesULRcH3oC-OyqX-Z7pPNA@mail.gmail.com>
X-Original-Sender: vicente.botet@wanadoo.fr
X-Original-Authentication-Results: mx.google.com;       spf=neutral
 (google.com: 80.12.242.125 is neither permitted nor denied by best guess
 record for domain of vicente.botet@wanadoo.fr) smtp.mail=vicente.botet@wanadoo.fr
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post?hl=en>,
 <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?hl=en&topic=25838>,
 <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe?hl=en>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe?hl=en>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:2702
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/2702>

Le 03/02/13 06:52, Jeffrey Yasskin a =E9crit :
> On Sat, Feb 2, 2013 at 10:00 AM, Howard Hinnant
> <howard.hinnant@gmail.com> wrote:
>> On Feb 2, 2013, at 12:53 PM, Ville Voutilainen <ville.voutilainen@gmail.=
com> wrote:
>>
>>> ----- Original message -----
>>>> Hi all -
>>>>
>>>> I'd like to propose the addition of two new type relationship traits t=
o
>>>> the C++ standard library:
>>>>
>>>> template<typename T, typename U =3D T>
>>>> struct std::is_swappable;
>>>>
>>>> template<typename T, typename U =3D T>
>>>> struct std::is_nothrow_swappable;
>>>>
>>>> The purpose of these two new traits is primarily to make it easier to
>>>> correctly query the noexcept status of swap when writing a noexcept
>>>> expression.
>>> Just quickly, what's the issue with
>>> noexcept(swap(x, y)) ?
>> Challenge:  Make this code compile without a using directive or using de=
claration:
>>
>> #include <algorithm>
>>
>> template <class T>
>> void
>> foo(T& x, T& y) noexcept(noexcept(swap(x, y)));
>>
>> struct A
>> {
>>      A() =3D default;
>>      A(const A&);
>>      A& operator=3D(const A&);
>> };
>>
>> void swap(A&, A&) noexcept;
>>
>> int main()
>> {
>>      int i;
>>      A a;
>>      static_assert(noexcept(foo(i, i)), "");
>>      static_assert(noexcept(foo(a, a)), "");
>> }
>>
>> Howard
> :)
>
> At one point, I think Dave suggested making ::std::begin(foo)
> automatically delegate to ADL when that's possible. I forget exactly
> how that worked, but if it's possible, we should consider doing it for
> swap() as well.
>
>
The following code based on the idea of boost::swap works with clang 3.2

"The template function boost::swap allows the values of two variables to=20
be swapped, using argument dependent lookup to select a specialized swap=20
function if available. If no specialized swap function is available,=20
std::swap is used."

namespace nstd_swap_impl
{
   using std::swap;

   template <class T>
   void swap_impl(T& x, T& y) noexcept(noexcept(swap(x, y)))
   {
       swap(x, y);
   }
}

namespace nstd
{
   template<class T1, class T2>
   void swap(T1& x, T2& y) noexcept(noexcept(::nstd_swap_impl::swap_impl(x,=
 y)))
   {
     ::nstd_swap_impl::swap_impl(x, y);
   }
}

template <class T>
void
foo(T& x, T& y) noexcept(noexcept(nstd::swap(x, y)));

struct A
{
     A() =3D default;
     A(const A&);
     A& operator=3D(const A&);
};

void swap(A&, A&) noexcept;

int main()
{
     int i;
     A a;
     static_assert(noexcept(foo(i, i)), "");
     static_assert(noexcept(foo(a, a)), "");
}

I don't know if Dave suggestion was related to this technique, but at least=
 the nstd::swap is reusable.

Vicente

P.S. I needed to rename swap on for make working gcc

namespace nstd
{
   template<class T1, class T2>
   void swap2(T1& x, T2& y) noexcept(noexcept(::nstd_swap_impl::swap_impl(x=
, y)))
   {
     ::nstd_swap_impl::swap_impl(x, y);
   }
}

template <class T>
void
foo(T& x, T& y) noexcept(noexcept(nstd::swap2(x, y)));


as otherwise

.../example/swap_test.cpp:19:77: error: template instantiation depth exceeds=
 maximum of 128 (use -ftemplate-depth=3D to increase the maximum) substitut=
ing =91template<class T> void nstd_swap_impl::swap_impl(T&, T&) [with T =3D=
 <missing>]=92
    void swap(T1& x, T2& y) noexcept(noexcept(::nstd_swap_impl::swap_impl(x=
, y)))
                                                                           =
   ^
.../example/swap_test.cpp:19:77:   required from =91void nstd::swap(T1&, T2&=
) [with T1 =3D int; T2 =3D int]=92
.../example/swap_test.cpp:27:50:   required from =91void nstd_swap_impl::swa=
p_impl(T&, T&) [with T =3D int]=92
.../example/swap_test.cpp:19:77:   required from =91void nstd::swap(T1&, T2&=
) [with T1 =3D int; T2 =3D int]=92
.../example/swap_test.cpp:27:50:   required from =91void nstd_swap_impl::swa=
p_impl(T&, T&) [with T =3D int]=92
.../example/swap_test.cpp:19:77:   required from =91void nstd::swap(T1&, T2&=
) [with T1 =3D int; T2 =3D int]=92
.../example/swap_test.cpp:27:50:   [ skipping 118 instantiation contexts, us=
e -ftemplate-backtrace-limit=3D0 to disable ]
.../example/swap_test.cpp:27:50:   required from =91void nstd_swap_impl::swa=
p_impl(T&, T&) [with T =3D int]=92
.../example/swap_test.cpp:19:77:   required from =91void nstd::swap(T1&, T2&=
) [with T1 =3D int; T2 =3D int]=92
.../example/swap_test.cpp:27:50:   required from =91void nstd_swap_impl::swa=
p_impl(T&, T&) [with T =3D int]=92
.../example/swap_test.cpp:19:77:   required from =91void nstd::swap(T1&, T2&=
) [with T1 =3D int; T2 =3D int]=92
.../example/swap_test.cpp:27:50:   required from =91void foo(T&, T&) [with T=
 =3D int]=92
.../example/swap_test.cpp:42:36:   required from here


--=20

---=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/?hl=3Den.



.
