libstdc++: Improvements to standard error category objects (part deux)

In r12-3860 the error categories in <system_error> were made final and
immortal, but I missed the categories for <future> and <ios>. This makes
the same changes to those.

libstdc++-v3/ChangeLog:

	* src/c++11/cxx11-ios_failure.cc (io_error_category): Define
	class and virtual functions as 'final'.
	(io_category_instance): Use constinit union to make the object
	immortal.
	* src/c++11/future.cc (future_error_category): Define class and
	virtual functions as 'final'.
	(future_category_instance): Use constinit union.
This commit is contained in:
Jonathan Wakely 2021-09-22 13:56:21 +01:00
parent 1918067e2d
commit 096228d84e
2 changed files with 31 additions and 20 deletions

View File

@ -44,14 +44,15 @@
namespace
{
struct io_error_category : std::error_category
struct io_error_category final : std::error_category
{
virtual const char*
name() const noexcept
const char*
name() const noexcept final
{ return "iostream"; }
_GLIBCXX_DEFAULT_ABI_TAG
virtual std::string message(int __ec) const
std::string
message(int __ec) const final
{
std::string __msg;
switch (std::io_errc(__ec))
@ -67,13 +68,17 @@ namespace
}
};
const io_error_category&
__io_category_instance() noexcept
struct constant_init
{
static const io_error_category __ec{};
return __ec;
}
union {
unsigned char unused;
io_error_category cat;
};
constexpr constant_init() : cat() { }
~constant_init() { /* do nothing, union member is not destroyed */ }
};
__constinit constant_init io_category_instance{};
} // namespace
namespace std _GLIBCXX_VISIBILITY(default)
@ -82,7 +87,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const error_category&
iostream_category() noexcept
{ return __io_category_instance(); }
{ return io_category_instance.cat; }
ios_base::failure::failure(const string& __str)
: system_error(io_errc::stream, __str) { }

View File

@ -27,14 +27,15 @@
namespace
{
struct future_error_category : public std::error_category
struct future_error_category final : public std::error_category
{
virtual const char*
name() const noexcept
const char*
name() const noexcept final
{ return "future"; }
_GLIBCXX_DEFAULT_ABI_TAG
virtual std::string message(int __ec) const
std::string
message(int __ec) const final
{
std::string __msg;
switch (std::future_errc(__ec))
@ -59,12 +60,17 @@ namespace
}
};
const future_error_category&
__future_category_instance() noexcept
struct constant_init
{
static const future_error_category __fec{};
return __fec;
}
union {
unsigned char unused;
future_error_category cat;
};
constexpr constant_init() : cat() { }
~constant_init() { /* do nothing, union member is not destroyed */ }
};
__constinit constant_init future_category_instance{};
}
namespace std _GLIBCXX_VISIBILITY(default)
@ -76,7 +82,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ _GLIBCXX_THROW_OR_ABORT(future_error(make_error_code(future_errc(__i)))); }
const error_category& future_category() noexcept
{ return __future_category_instance(); }
{ return future_category_instance.cat; }
future_error::~future_error() noexcept { }