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