]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/exchange/1.cc
1 // { dg-options "-std=gnu++1y" }
3 // Copyright (C) 2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 // 20.2.3 exchange [utility.exchange]
23 #include <type_traits>
24 #include <testsuite_hooks.h>
29 const unsigned val
= 4;
31 auto prev
= std::exchange(i
, val
);
32 static_assert( std::is_same
<decltype(prev
), int>::value
, "return type" );
35 prev
= std::exchange(i
, 3);
40 // Default construction from empty braces
44 bool test
__attribute__((unused
)) = true;
46 struct DefaultConstructible
48 DefaultConstructible(int i
= 0) : value(i
) { }
52 DefaultConstructible x
= 1;
53 auto old
= std::exchange(x
, {});
54 VERIFY( x
.value
== 0 );
55 VERIFY( old
.value
== 1 );
58 int f(int) { return 0; }
60 double f(double) { return 0; }
62 // Deduce type of overloaded function
66 bool test
__attribute__((unused
)) = true;
69 std::exchange(fp
, &f
);
70 VERIFY( fp
!= nullptr );
79 To(const To
&) = default;
80 To(const From
&) = delete;
81 To
& operator=(const From
&) { value
= 1; }
82 To
& operator=(From
&&) { value
= 2; }
88 auto prev
= std::exchange(t
, f
);
89 VERIFY( t
.value
== 1 );
90 VERIFY( prev
.value
== 0 );
92 prev
= std::exchange(t
, From
{});
93 VERIFY( t
.value
== 2 );
94 VERIFY( prev
.value
== 1 );