1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <locale>
11
12// template <class Facet> const Facet& use_facet(const locale& loc);
13
14#include <locale>
15#include <cassert>
16
17int facet_count = 0;
18
19struct my_facet
20    : public std::locale::facet
21{
22    static std::locale::id id;
23
24    bool im_alive;
25
26    my_facet() : im_alive(true) {++facet_count;}
27    ~my_facet() {im_alive = false; --facet_count;}
28};
29
30std::locale::id my_facet::id;
31
32int main()
33{
34    try
35    {
36        const my_facet& f = std::use_facet<my_facet>(std::locale());
37        assert(false);
38    }
39    catch (std::bad_cast&)
40    {
41    }
42    const my_facet* fp = 0;
43    {
44        std::locale loc(std::locale(), new my_facet);
45        const my_facet& f = std::use_facet<my_facet>(loc);
46        assert(f.im_alive);
47        fp = &f;
48        assert(fp->im_alive);
49        assert(facet_count == 1);
50    }
51    assert(facet_count == 0);
52}
53