]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/any/misc/any_cast.cc
1 // { dg-options "-std=gnu++14" }
4 // Copyright (C) 2014-2016 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 #include <experimental/any>
24 #include <testsuite_hooks.h>
26 using std::experimental::any
;
27 using std::experimental::any_cast
;
34 // taken from example in N3804 proposal
36 any
x(5); // x holds int
37 VERIFY(any_cast
<int>(x
) == 5); // cast to value
38 any_cast
<int&>(x
) = 10; // cast to reference
39 VERIFY(any_cast
<int>(x
) == 10);
41 x
= "Meow"; // x holds const char*
42 VERIFY(strcmp(any_cast
<const char*>(x
), "Meow") == 0);
43 any_cast
<const char*&>(x
) = "Harry";
44 VERIFY(strcmp(any_cast
<const char*>(x
), "Harry") == 0);
46 x
= string("Meow"); // x holds string
48 s
= move(any_cast
<string
&>(x
)); // move from any
50 any_cast
<string
&>(x
) = move(s2
); // move to any
51 VERIFY(any_cast
<const string
&>(x
) == "Jane");
54 const any
y(cat
); // const y holds string
55 VERIFY(any_cast
<const string
&>(y
) == cat
);
60 using std::experimental::bad_any_cast
;
62 auto p
= any_cast
<double>(&x
);
66 p
= any_cast
<double>(&x
);
70 p
= any_cast
<double>(&x
);
76 } catch (const bad_any_cast
&) {