From patchwork Sat May 18 16:22:47 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 244764 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id E68F92C009D for ; Sun, 19 May 2013 02:23:04 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:date:message-id:subject:from:to:content-type; q= dns; s=default; b=tZ6UoqDcWwB04dSXrQYf/0zcy5voj0WtkOjXsIXcvzT+Oe yXBoofImWf4RpL/41EV1usel5K/tsXz40QJR0yYtgNrKPfFIE+fPnwL4iGKCAlcq SbtNJVPO1wuIUYvG346ytNE2SVf7mH2a3nEXEIuXT5RKMq1Qf7jgn0nRkx0ps= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:date:message-id:subject:from:to:content-type; s= default; bh=UpASmzN/m7lregaWU8BcJXdnNm0=; b=H3pCi0BfxhTynZaVSDa6 SI4YQoUgDUKTdeKsF1yZ3kpDSe0Xx7k4+s1bk8DsR3XCyv2KRs6vsKAgne5tfldF okQz/tsTxAXEwrxzcXtpM3hcwoKog3Vl3JiD5d7FFN8WCgwnuUncMMzaKftFRssJ 0MpcfZxT+secRim+o+S81qI= Received: (qmail 12858 invoked by alias); 18 May 2013 16:22:59 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 12815 invoked by uid 89); 18 May 2013 16:22:51 -0000 X-Spam-SWARE-Status: No, score=-2.8 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, SPF_PASS autolearn=ham version=3.3.1 X-Spam-User: qpsmtpd, 2 recipients Received: from mail-la0-f50.google.com (HELO mail-la0-f50.google.com) (209.85.215.50) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Sat, 18 May 2013 16:22:50 +0000 Received: by mail-la0-f50.google.com with SMTP id ed20so5131433lab.23 for ; Sat, 18 May 2013 09:22:47 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.112.159.10 with SMTP id wy10mr6562489lbb.95.1368894167395; Sat, 18 May 2013 09:22:47 -0700 (PDT) Received: by 10.112.166.101 with HTTP; Sat, 18 May 2013 09:22:47 -0700 (PDT) Date: Sat, 18 May 2013 17:22:47 +0100 Message-ID: Subject: [patch] implement std::exchange From: Jonathan Wakely To: "libstdc++" , gcc-patches X-Virus-Found: No * include/std/utility (exchange): Define. * testsuite/20_util/exchange/1.cc: New. Tested x86_64-linux, committed to trunk. commit f368932e633134e5fb50a1e7d6ffcf6aa4b79b0d Author: Jonathan Wakely Date: Sat May 18 16:27:13 2013 +0100 * include/std/utility (exchange): Define. * testsuite/20_util/exchange/1.cc: New. diff --git a/libstdc++-v3/include/std/utility b/libstdc++-v3/include/std/utility index 8142ea4..ee8c6b1 100644 --- a/libstdc++-v3/include/std/utility +++ b/libstdc++-v3/include/std/utility @@ -152,6 +152,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION get(const std::pair<_Tp1, _Tp2>& __in) noexcept { return __pair_get<_Int>::__const_get(__in); } +#if __cplusplus > 201103L + /// Assign @p __new_val to @p __obj and return its previous value. + template + inline _Tp + exchange(_Tp& __obj, _Up&& __new_val) + { + _Tp __old_val = std::move(__obj); + __obj = std::forward<_Up>(__new_val); + return __old_val; + } +#endif + _GLIBCXX_END_NAMESPACE_VERSION } // namespace diff --git a/libstdc++-v3/testsuite/20_util/exchange/1.cc b/libstdc++-v3/testsuite/20_util/exchange/1.cc new file mode 100644 index 0000000..d16d9e9 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/exchange/1.cc @@ -0,0 +1,103 @@ +// { dg-options "-std=gnu++1y" } + +// Copyright (C) 2013 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// 20.2.3 exchange [utility.exchange] + +#include +#include +#include + +void +test01() +{ + const unsigned val = 4; + int i = 1; + auto prev = std::exchange(i, val); + static_assert( std::is_same::value, "return type" ); + VERIFY( i == 4 ); + VERIFY( prev == 1 ); + prev = std::exchange(i, 3); + VERIFY( i == 3 ); + VERIFY( prev == 4 ); +} + +// Default construction from empty braces +void +test02() +{ + bool test __attribute__((unused)) = true; + + struct DefaultConstructible + { + DefaultConstructible(int i = 0) : value(i) { } + int value; + }; + + DefaultConstructible x = 1; + auto old = std::exchange(x, {}); + VERIFY( x.value == 0 ); + VERIFY( old.value == 1 ); +} + +// Deduce type of overloaded function +void +test03() +{ + bool test __attribute__((unused)) = true; + + int (*fp)(int); + int f(int); + double f(double); + std::exchange(fp, &f); + VERIFY( fp != nullptr ); +} + +void test04() +{ + struct From { }; + struct To { + int value = 0; + To() = default; + To(const To&) = default; + To(const From&) = delete; + To& operator=(const From&) { value = 1; } + To& operator=(From&&) { value = 2; } + }; + + To t; + From f; + + auto prev = std::exchange(t, f); + VERIFY( t.value == 1 ); + VERIFY( prev.value == 0 ); + + prev = std::exchange(t, From{}); + VERIFY( t.value == 2 ); + VERIFY( prev.value == 1 ); +} + +int +main() +{ + test01(); + test02(); + test03(); + test04(); + return 0; +}