]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/exchange/1.cc
Use effective-target instead of -std options
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / exchange / 1.cc
CommitLineData
52066eae 1// { dg-do run { target c++14 } }
c98b0598 2
818ab71a 3// Copyright (C) 2013-2016 Free Software Foundation, Inc.
c98b0598
JW
4//
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)
9// any later version.
10
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.
15
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/>.
19
20// 20.2.3 exchange [utility.exchange]
21
22#include <utility>
23#include <type_traits>
24#include <testsuite_hooks.h>
25
26void
27test01()
28{
29 const unsigned val = 4;
30 int i = 1;
31 auto prev = std::exchange(i, val);
32 static_assert( std::is_same<decltype(prev), int>::value, "return type" );
33 VERIFY( i == 4 );
34 VERIFY( prev == 1 );
35 prev = std::exchange(i, 3);
36 VERIFY( i == 3 );
37 VERIFY( prev == 4 );
38}
39
40// Default construction from empty braces
41void
42test02()
43{
44 bool test __attribute__((unused)) = true;
45
46 struct DefaultConstructible
47 {
48 DefaultConstructible(int i = 0) : value(i) { }
49 int value;
50 };
51
52 DefaultConstructible x = 1;
53 auto old = std::exchange(x, {});
54 VERIFY( x.value == 0 );
55 VERIFY( old.value == 1 );
56}
57
37ea0d87
DN
58int f(int) { return 0; }
59
60double f(double) { return 0; }
61
c98b0598
JW
62// Deduce type of overloaded function
63void
64test03()
65{
66 bool test __attribute__((unused)) = true;
67
68 int (*fp)(int);
c98b0598
JW
69 std::exchange(fp, &f);
70 VERIFY( fp != nullptr );
71}
72
73void test04()
74{
75 struct From { };
76 struct To {
77 int value = 0;
78 To() = default;
79 To(const To&) = default;
80 To(const From&) = delete;
631e3e1b
JW
81 To& operator=(const From&) { value = 1; return *this; }
82 To& operator=(From&&) { value = 2; return *this; }
c98b0598
JW
83 };
84
85 To t;
86 From f;
87
88 auto prev = std::exchange(t, f);
89 VERIFY( t.value == 1 );
90 VERIFY( prev.value == 0 );
91
92 prev = std::exchange(t, From{});
93 VERIFY( t.value == 2 );
94 VERIFY( prev.value == 1 );
95}
96
97int
98main()
99{
100 test01();
101 test02();
102 test03();
103 test04();
104 return 0;
105}