summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/move_only_function/move.cc
blob: 0bed6edbf1a7b1ea008954c2acb30ee5c7470a84 (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
106
107
108
109
110
// { dg-options "-std=gnu++23" }
// { dg-do run { target c++23 } }
// { dg-require-effective-target hosted }

#include <functional>
#include <testsuite_hooks.h>

using std::move_only_function;

void
test01()
{
  // Small type with non-throwing move constructor. Not allocated on the heap.
  struct F
  {
    F() = default;
    F(const F& f) : counters(f.counters) { ++counters.copy; }
    F(F&& f) noexcept : counters(f.counters) { ++counters.move; }

    F& operator=(F&&) = delete;

    struct Counters
    {
      int copy = 0;
      int move = 0;
    } counters;

    const Counters& operator()() const { return counters; }
  };

  F f;
  std::move_only_function<const F::Counters&() const> m1(f);
  VERIFY( m1().copy == 1 );
  VERIFY( m1().move == 0 );

  // This will move construct a new target object and destroy the old one:
  auto m2 = std::move(m1);
  VERIFY( m1 == nullptr && m2 != nullptr );
  VERIFY( m2().copy == 1 );
  VERIFY( m2().move == 1 );

  m1 = std::move(m2);
  VERIFY( m1 != nullptr && m2 == nullptr );
  VERIFY( m1().copy == 1 );
  VERIFY( m1().move == 2 );

  m2 = std::move(f);
  VERIFY( m2().copy == 0 );
  VERIFY( m2().move == 2 ); // Move construct target object, then swap into m2.
  const int moves = m1().move + m2().move;
  // This will do three moves:
  swap(m1, m2);
  VERIFY( m1().copy == 0 );
  VERIFY( m2().copy == 1 );
  VERIFY( (m1().move + m2().move) == (moves + 3) );
}

void
test02()
{
  // Move constructor is potentially throwing. Allocated on the heap.
  struct F
  {
    F() = default;
    F(const F& f) noexcept : counters(f.counters) { ++counters.copy; }
    F(F&& f) noexcept(false) : counters(f.counters) { ++counters.move; }

    F& operator=(F&&) = delete;

    struct Counters
    {
      int copy = 0;
      int move = 0;
    } counters;

    Counters operator()() const noexcept { return counters; }
  };

  F f;
  std::move_only_function<F::Counters() const> m1(f);
  VERIFY( m1().copy == 1 );
  VERIFY( m1().move == 0 );

  // The target object is on the heap so this just moves a pointer:
  auto m2 = std::move(m1);
  VERIFY( m1 == nullptr && m2 != nullptr );
  VERIFY( m2().copy == 1 );
  VERIFY( m2().move == 0 );

  m1 = std::move(m2);
  VERIFY( m1 != nullptr && m2 == nullptr );
  VERIFY( m1().copy == 1 );
  VERIFY( m1().move == 0 );

  m2 = std::move(f);
  VERIFY( m2().copy == 0 );
  VERIFY( m2().move == 1 );
  const int moves = m1().move + m2().move;
  // This just swaps the pointers, so no moves:
  swap(m1, m2);
  VERIFY( m1().copy == 0 );
  VERIFY( m2().copy == 1 );
  VERIFY( (m1().move + m2().move) == moves );
}

int main()
{
  test01();
  test02();
}