Home | History | Annotate | Download | only in locale.global.templates
      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 
     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 
     28     my_facet() : im_alive(true) {++facet_count;}
     29     ~my_facet() {im_alive = false; --facet_count;}
     30 };
     31 
     32 std::locale::id my_facet::id;
     33 
     34 int main()
     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