[libcxx-commits] [libcxx] [libc++] Avoid instantiating __copy_cv_t for every type (PR #222647)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Thu Sep 10 06:15:08 PDT 2026


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/222647

We can instead use `__is_const` and `__is_volatile` and dispatch to a
specialization based on that, brining the total number of instantiations
of `__copy_cv_impl` down to 4.


>From d4ec1e95e3b4f3f8fc76b5fed449a3401ddb8e9c Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 10 Sep 2026 15:05:48 +0200
Subject: [PATCH] [libc++] Avoid instantiating __copy_cv_t for every type

We can instead use `__is_const` and `__is_volatile` and dispatch to a
specialization based on that, brining the total number of instantiations
of `__copy_cv_impl` down to 4.
---
 libcxx/include/__type_traits/copy_cv.h | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/libcxx/include/__type_traits/copy_cv.h b/libcxx/include/__type_traits/copy_cv.h
index 8378fbd50ef53..14dbf5a9789d3 100644
--- a/libcxx/include/__type_traits/copy_cv.h
+++ b/libcxx/include/__type_traits/copy_cv.h
@@ -19,32 +19,36 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 // Let COPYCV(FROM, TO) be an alias for type TO with the addition of FROM's
 // top-level cv-qualifiers.
-template <class _From>
-struct __copy_cv {
+template <bool, bool>
+struct __copy_cv_impl;
+
+template <>
+struct __copy_cv_impl<false, false> {
   template <class _To>
   using __apply _LIBCPP_NODEBUG = _To;
 };
 
-template <class _From>
-struct __copy_cv<const _From> {
+template <>
+struct __copy_cv_impl<true, false> {
   template <class _To>
   using __apply _LIBCPP_NODEBUG = const _To;
 };
 
-template <class _From>
-struct __copy_cv<volatile _From> {
+template <>
+struct __copy_cv_impl<false, true> {
   template <class _To>
   using __apply _LIBCPP_NODEBUG = volatile _To;
 };
 
-template <class _From>
-struct __copy_cv<const volatile _From> {
+template <>
+struct __copy_cv_impl<true, true> {
   template <class _To>
   using __apply _LIBCPP_NODEBUG = const volatile _To;
 };
 
 template <class _From, class _To>
-using __copy_cv_t _LIBCPP_NODEBUG = typename __copy_cv<_From>::template __apply<_To>;
+using __copy_cv_t _LIBCPP_NODEBUG =
+    typename __copy_cv_impl<__is_const(_From), __is_volatile(_From)>::template __apply<_To>;
 
 _LIBCPP_END_NAMESPACE_STD
 



More information about the libcxx-commits mailing list