openMSX
semiregular_test.cc
Go to the documentation of this file.
1 #include "catch.hpp"
2 #include "semiregular.hh"
3 #include <memory>
4 
5 
6 // Mockup for an transform-iterator containing a transformation function (e.g.
7 // a lambda).
8 template <typename Op> struct Iter {
9  Iter() = default;
10  Iter(Op op_) : op(std::move(op_)) {}
11  semiregular_t<Op> op; // wrap 'Op' in semiregular_t<T>
12 };
13 
14 // Factory function for iterator
15 template <typename Op> auto make_iter(Op op)
16 {
17  return Iter<Op>(std::move(op));
18 }
19 
20 TEST_CASE("semiregular")
21 {
22  // This doesn't CHECK() anything, it only tests that the code compiles.
23  SECTION("semiregular_t<lambda>") {
24  int g = 42;
25 
26  // create dummy lambda, captures 'g' by-reference.
27  auto lambda = [&](int i) { return i + g; };
28  auto l2 = lambda; (void)l2; // lambda is copy-constructible
29  // l2 = lambda; // ok, doesn't compile: lambda is not copy-assignable
30  // decltype(lambda) l3; // ok, doesn't compile: not default-constructible
31 
32  // create iterator containing this lambda
33  auto iter1 = make_iter(lambda);
34  auto iter2 = iter1; // ok, copy-constructible
35  iter2 = iter1; // ok, copy-assignable
36  decltype(iter1) iter3; // ok, default-constructible
37  (void)iter2; (void)iter3;
38  }
39  SECTION("semiregular_t<unique_ptr<int>>") {
40  // unique_ptr
41  using T = std::unique_ptr<int>;
42  T t1 = std::make_unique<int>(43);
43  // T t2 = t1; // ok, doesn't compile: move-only
44 
45  // wrapped in semiregular_t<T>
46  using ST = semiregular_t<T>;
47  ST st1 = std::move(t1);
48  // ST st2 = st1; ok, doesn't compile because move-only
49  ST st2 = std::move(st1);
50  st1 = std::move(st2);
51  }
52 }
semiregular_t< Op > op
std::conditional_t< std::is_default_constructible< T >::value &&std::is_copy_assignable< T >::value, T, sreg_impl::semiregular< T > > semiregular_t
Definition: semiregular.hh:179
Iter(Op op_)
STL namespace.
TEST_CASE("semiregular")
int g
Iter()=default
auto make_iter(Op op)