re PR c++/51459 ('double free or corruption' involving std::function and lambdas)
[platform/upstream/gcc.git] / gcc / testsuite / g++.dg / cpp0x / lambda / lambda-template4.C
1 // PR c++/51459
2 // { dg-do run { target c++11 } }
3
4 struct func {
5     virtual ~func() { }
6     virtual void operator()() const = 0;
7     virtual func* clone() const = 0;
8 };
9
10 template<typename T>
11 struct funcimpl : func {
12     explicit funcimpl(T t) : t(t) { }
13     void operator()() const { t(); }
14     func* clone() const { return new funcimpl(*this); }
15     T t;
16 };
17
18 struct function
19 {
20     func* p;
21
22     template<typename T>
23         function(T t) : p(new funcimpl<T>(t)) { }
24
25     ~function() { delete p; }
26
27     function(const function& f) : p(f.p->clone()) { }
28
29     function& operator=(const function& ) = delete;
30
31     void operator()() const { (*p)(); }
32 };
33
34 template <typename F>
35 function animate(F f) { return [=]{ f(); }; }
36
37 int main()
38 {
39   function linear1 = []{};
40   function av(animate(linear1));
41   av();
42 }