1 // Ensure that generic lambdas properly construct and destroy user types.
2 // { dg-options "-DUSE_AUTO_SYNTAX" }
3 // { dg-do run { target c++14 } }
4 
5 int i = 3;
6 
7 struct S
8 {
SS9   S () { ++i; }
SS10   S (S const&) { ++i; }
SS11   S (S&& old) { old.shadow = true; i += 2; }
~SS12   ~S () { if (shadow) i -= 2; else --i; }
13 
14   bool shadow = false;
15 };
16 
17 extern "C" int printf(const char*, ...);
18 #define assert(e) if (e); else \
19 		 printf ("%s:%d: !(%s)\n", __FILE__, __LINE__, #e), __builtin_abort ();
20 
main()21 int main ()
22 {
23   assert (i == 3);
24   {
25     S s; assert (i == 4);
26 
27     #if USE_AUTO_SYNTAX
28     auto byref = [] (auto& r)                   { (void) r; };
29     auto bycref = [] (auto const& r)            { (void) r; };
30     auto byval = [] (auto v, auto const x)      { assert (i == x); (void) v; };
31     auto byrval = [] (auto&& r, auto const x)   { S steal (static_cast<S&&>(r));
32 		 		 		 		 		           assert (i == x); };
33 
34     #elif USE_EXPLICIT_TEMPLATE_SYNTAX
35     auto byref = [] <typename T> (T& r)         { (void) r; };
36     auto bycref = [] <typename T> (T const& r)  { (void) r; };
37     auto byval = [] <typename T, typename I>
38 		 		     (T v, I const x)		 		 { assert (i == x); (void) v; };
39     auto byrval = [] <typename T, typename I>
40 		 		      (T&& r, I const x)		 		 { S steal (static_cast<S&&>(r));
41 		 		 		 		 		           assert (i == x); };
42     #endif
43 
44     byref (s); assert (i == 4);
45     bycref (s); assert (i == 4);
46     byval (s, 5); assert (i == 4);
47     byrval (static_cast<S&&>(s), 6); assert (i == 5);
48   }
49   assert (i == 3);
50 }
51 
52