Index: libcxx/src/future.cpp =================================================================== --- libcxx/src/future.cpp +++ libcxx/src/future.cpp @@ -15,6 +15,12 @@ _LIBCPP_BEGIN_NAMESPACE_STD +// Attempt to ensure that the error_category singleton object is within its +// lifetime when accessed by the user program (even during the program +// termination) by referencing it in the initialization of a non-local +// variable of static storage duration. +static const error_category *__error_category_future = &future_category(); + class _LIBCPP_HIDDEN __future_error_category : public __do_message { Index: libcxx/src/ios.cpp =================================================================== --- libcxx/src/ios.cpp +++ libcxx/src/ios.cpp @@ -25,6 +25,12 @@ _LIBCPP_BEGIN_NAMESPACE_STD +// Attempt to ensure that the error_category singleton object is within its +// lifetime when accessed by the user program (even during the program +// termination) by referencing it in the initialization of a non-local +// variable of static storage duration. +static const error_category *__error_category_iostream = &iostream_category(); + template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ios; template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ios; Index: libcxx/src/system_error.cpp =================================================================== --- libcxx/src/system_error.cpp +++ libcxx/src/system_error.cpp @@ -25,6 +25,13 @@ _LIBCPP_BEGIN_NAMESPACE_STD +// Attempt to ensure that the error_category singleton object is within its +// lifetime when accessed by the user program (even during the program +// termination) by referencing it in the initialization of a non-local +// variable of static storage duration. +static const error_category *__error_category_generic = &generic_category(); +static const error_category *__error_category_system = &system_category(); + // class error_category #if defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS) Index: libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp =================================================================== --- libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp +++ libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/generic_category.pass.cpp @@ -27,6 +27,14 @@ #include "test_macros.h" +struct A { + const std::error_category* ec; + ~A() { + std::string str = ec->name(); + assert(str == "generic") ; + } +} a; + void test_message_for_bad_value() { errno = E2BIG; // something that message will never generate const std::error_category& e_cat1 = std::generic_category(); @@ -44,5 +52,9 @@ test_message_for_bad_value(); } + a.ec = &std::generic_category(); + std::string m2 = a.ec->name(); + assert(m2 == "generic"); + return 0; } Index: libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp =================================================================== --- libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp +++ libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp @@ -27,6 +27,14 @@ #include "test_macros.h" +struct A { + const std::error_category* ec; + ~A() { + std::string str = ec->name(); + assert(str == "system") ; + } +} a; + void test_message_for_bad_value() { errno = E2BIG; // something that message will never generate const std::error_category& e_cat1 = std::system_category(); @@ -48,5 +56,9 @@ test_message_for_bad_value(); } + a.ec = &std::system_category(); + std::string m = a.ec->name(); + assert(m == "system"); + return 0; } Index: libcxx/test/std/input.output/iostreams.base/std.ios.manip/error.reporting/iostream_category.pass.cpp =================================================================== --- libcxx/test/std/input.output/iostreams.base/std.ios.manip/error.reporting/iostream_category.pass.cpp +++ libcxx/test/std/input.output/iostreams.base/std.ios.manip/error.reporting/iostream_category.pass.cpp @@ -16,11 +16,23 @@ #include "test_macros.h" +struct A { + const std::error_category* ec; + ~A() { + std::string str = ec->name(); + assert(str == "iostream") ; + } +} a; + int main(int, char**) { const std::error_category& e_cat1 = std::iostream_category(); std::string m1 = e_cat1.name(); assert(m1 == "iostream"); + a.ec = &std::iostream_category(); + std::string m2 = a.ec->name(); + assert(m2 == "iostream"); + return 0; } Index: libcxx/test/std/thread/futures/futures.errors/future_category.pass.cpp =================================================================== --- libcxx/test/std/thread/futures/futures.errors/future_category.pass.cpp +++ libcxx/test/std/thread/futures/futures.errors/future_category.pass.cpp @@ -18,10 +18,20 @@ #include "test_macros.h" +struct A { + const std::error_category* ec; + ~A() { + assert(std::strcmp(ec->name(), "future") == 0); + } +} a; + int main(int, char**) { const std::error_category& ec = std::future_category(); assert(std::strcmp(ec.name(), "future") == 0); + a.ec = &std::future_category(); + assert(std::strcmp(a.ec->name(), "future") == 0); + return 0; }