xref: /llvm-project/libcxx/test/std/localization/locales/locale.global.templates/use_facet.pass.cpp (revision d5e26775d089d099d02b168de8ea52179b1b1f75)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <locale>
10 
11 // template <class Facet> const Facet& use_facet(const locale& loc);
12 
13 #include <locale>
14 #include <cassert>
15 #include <typeinfo>
16 
17 #include "test_macros.h"
18 
19 int facet_count = 0;
20 
21 struct my_facet
22     : public std::locale::facet
23 {
24     static std::locale::id id;
25 
26     bool im_alive;
27 
my_facetmy_facet28     my_facet() : im_alive(true) {++facet_count;}
~my_facetmy_facet29     ~my_facet() {im_alive = false; --facet_count;}
30 };
31 
32 std::locale::id my_facet::id;
33 
main(int,char **)34 int main(int, char**)
35 {
36 #ifndef TEST_HAS_NO_EXCEPTIONS
37     try
38     {
39         const my_facet& f = std::use_facet<my_facet>(std::locale());
40         ((void)f); // Prevent unused warning
41         assert(false);
42     }
43     catch (std::bad_cast&)
44     {
45     }
46 #endif
47     const my_facet* fp = 0;
48     {
49         std::locale loc(std::locale(), new my_facet);
50         const my_facet& f = std::use_facet<my_facet>(loc);
51         assert(f.im_alive);
52         fp = &f;
53         assert(fp->im_alive);
54         assert(facet_count == 1);
55     }
56     assert(facet_count == 0);
57 
58   return 0;
59 }
60