diff mbox series

libstdc++: Optimize __try_use_facet for const types

Message ID 20240801213927.388966-5-jwakely@redhat.com
State New
Headers show
Series libstdc++: Optimize __try_use_facet for const types | expand

Commit Message

Jonathan Wakely Aug. 1, 2024, 9:32 p.m. UTC
Tested x86_64-linux.

-- >8 --

LWG 436 confirmed that const-qualified types are valid arguments for
Facet template parameters, but volatile-qualified types are not.

libstdc++-v3/ChangeLog:

	* include/bits/locale_classes.h:
	* include/bits/locale_classes.tcc:
---
 libstdc++-v3/include/bits/locale_classes.h   | 6 ++++++
 libstdc++-v3/include/bits/locale_classes.tcc | 6 +++++-
 2 files changed, 11 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/locale_classes.h b/libstdc++-v3/include/bits/locale_classes.h
index 50a748066f1..2bc505c632a 100644
--- a/libstdc++-v3/include/bits/locale_classes.h
+++ b/libstdc++-v3/include/bits/locale_classes.h
@@ -381,6 +381,12 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #endif
   };
 
+#ifdef __cpp_variable_templates
+  template<typename _Tp>
+    constexpr bool __is_facet = __is_base_of(locale::facet, _Tp);
+  template<typename _Tp>
+    constexpr bool __is_facet<volatile _Tp> = false;
+#endif
 
   // 22.1.1.1.2  Class locale::facet
   /**
diff --git a/libstdc++-v3/include/bits/locale_classes.tcc b/libstdc++-v3/include/bits/locale_classes.tcc
index c79574e58de..58121835586 100644
--- a/libstdc++-v3/include/bits/locale_classes.tcc
+++ b/libstdc++-v3/include/bits/locale_classes.tcc
@@ -71,6 +71,10 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
     locale::
     combine(const locale& __other) const
     {
+#ifdef __cpp_variable_templates
+      static_assert(__is_facet<_Facet>, "");
+#endif
+
       _Impl* __tmp = new _Impl(*_M_impl, 1);
       __try
 	{
@@ -110,7 +114,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       // We know these standard facets are always installed in every locale
       // so dynamic_cast always succeeds, just use static_cast instead.
 #define _GLIBCXX_STD_FACET(...) \
-      if _GLIBCXX_CONSTEXPR (__is_same(_Facet, __VA_ARGS__)) \
+      if _GLIBCXX_CONSTEXPR (__is_same(const _Facet, const __VA_ARGS__)) \
 	return static_cast<const _Facet*>(__facets[__i])
 
       _GLIBCXX_STD_FACET(ctype<char>);