• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "test_macros.h"
17 
18 int facet_count = 0;
19 
20 struct my_facet
21     : public std::locale::facet
22 {
23     static std::locale::id id;
24 
25     bool im_alive;
26 
my_facetmy_facet27     my_facet() : im_alive(true) {++facet_count;}
~my_facetmy_facet28     ~my_facet() {im_alive = false; --facet_count;}
29 };
30 
31 std::locale::id my_facet::id;
32 
main(int,char **)33 int main(int, char**)
34 {
35 #ifndef TEST_HAS_NO_EXCEPTIONS
36     try
37     {
38         const my_facet& f = std::use_facet<my_facet>(std::locale());
39         ((void)f); // Prevent unused warning
40         assert(false);
41     }
42     catch (std::bad_cast&)
43     {
44     }
45 #endif
46     const my_facet* fp = 0;
47     {
48         std::locale loc(std::locale(), new my_facet);
49         const my_facet& f = std::use_facet<my_facet>(loc);
50         assert(f.im_alive);
51         fp = &f;
52         assert(fp->im_alive);
53         assert(facet_count == 1);
54     }
55     assert(facet_count == 0);
56 
57   return 0;
58 }
59