blob: 923920edda777f659adaa21e3754f8b6c95ec5ed (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// { dg-options "-std=gnu++1y" }
// Copyright (C) 2013-2014 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
// <http://www.gnu.org/licenses/>.
// 20.2.3 exchange [utility.exchange]
#include <utility>
#include <type_traits>
#include <testsuite_hooks.h>
void
test01()
{
const unsigned val = 4;
int i = 1;
auto prev = std::exchange(i, val);
static_assert( std::is_same<decltype(prev), int>::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 );
}
int f(int) { return 0; }
double f(double) { return 0; }
// Deduce type of overloaded function
void
test03()
{
bool test __attribute__((unused)) = true;
int (*fp)(int);
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;
}
|