[libcxx-commits] [libcxx] [libc++] Prohibits initializer_list specializations. (PR #128042)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Feb 20 10:18:47 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

<details>
<summary>Changes</summary>

This relies on Clang's no_specializations attribute which is not supported by GCC.

Implements:
- LWG2129: User specializations of std::initializer_list

Fixes: #<!-- -->126270

---
Full diff: https://github.com/llvm/llvm-project/pull/128042.diff


3 Files Affected:

- (modified) libcxx/docs/Status/Cxx17Issues.csv (+1-1) 
- (modified) libcxx/include/initializer_list (+1-1) 
- (added) libcxx/test/std/language.support/support.initlist/support.initlist.syn/specialization.verify.cpp (+24) 


``````````diff
diff --git a/libcxx/docs/Status/Cxx17Issues.csv b/libcxx/docs/Status/Cxx17Issues.csv
index 477f3d363a4e2..d09b37f1d3900 100644
--- a/libcxx/docs/Status/Cxx17Issues.csv
+++ b/libcxx/docs/Status/Cxx17Issues.csv
@@ -12,7 +12,7 @@
 "`LWG2404 <https://wg21.link/LWG2404>`__","``mismatch()``\ 's complexity needs to be updated","2014-11 (Urbana)","|Complete|","",""
 "`LWG2408 <https://wg21.link/LWG2408>`__","SFINAE-friendly ``common_type``\  / ``iterator_traits``\  is missing in C++14","2014-11 (Urbana)","|Complete|","",""
 "`LWG2106 <https://wg21.link/LWG2106>`__","``move_iterator``\  wrapping iterators returning prvalues","2014-11 (Urbana)","|Complete|","",""
-"`LWG2129 <https://wg21.link/LWG2129>`__","User specializations of ``std::initializer_list``\ ","2014-11 (Urbana)","|Complete|","",""
+"`LWG2129 <https://wg21.link/LWG2129>`__","User specializations of ``std::initializer_list``\ ","2014-11 (Urbana)","|Complete|","21",""
 "`LWG2212 <https://wg21.link/LWG2212>`__","``tuple_size``\  for ``const pair``\  request <tuple> header","2014-11 (Urbana)","|Complete|","",""
 "`LWG2217 <https://wg21.link/LWG2217>`__","``operator==(sub_match, string)``\  slices on embedded '\0's","2014-11 (Urbana)","|Complete|","",""
 "`LWG2230 <https://wg21.link/LWG2230>`__","""see below"" for ``initializer_list``\  constructors of unordered containers","2014-11 (Urbana)","|Complete|","",""
diff --git a/libcxx/include/initializer_list b/libcxx/include/initializer_list
index 07c51f32fee7d..3967ad8aaef7c 100644
--- a/libcxx/include/initializer_list
+++ b/libcxx/include/initializer_list
@@ -59,7 +59,7 @@ namespace std // purposefully not versioned
 #  ifndef _LIBCPP_CXX03_LANG
 
 template <class _Ep>
-class _LIBCPP_TEMPLATE_VIS initializer_list {
+class _LIBCPP_TEMPLATE_VIS _LIBCPP_NO_SPECIALIZATIONS initializer_list {
   const _Ep* __begin_;
   size_t __size_;
 
diff --git a/libcxx/test/std/language.support/support.initlist/support.initlist.syn/specialization.verify.cpp b/libcxx/test/std/language.support/support.initlist/support.initlist.syn/specialization.verify.cpp
new file mode 100644
index 0000000000000..5ea36a47cee1a
--- /dev/null
+++ b/libcxx/test/std/language.support/support.initlist/support.initlist.syn/specialization.verify.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03
+
+// template<class E> class initializer_list;
+//
+// If an explicit specialization or partial specialization of initializer_list
+// is declared, the program is ill-formed.
+
+#include <initializer_list>
+
+// expected-error at +2 {{'initializer_list' cannot be specialized: Users are not allowed to specialize this standard library entity}}
+template <>
+class std::initializer_list<int> {};
+
+// expected-error at +2 {{'initializer_list' cannot be specialized: Users are not allowed to specialize this standard library entity}}
+template <typename T>
+class std::initializer_list<T*> {};

``````````

</details>


https://github.com/llvm/llvm-project/pull/128042


More information about the libcxx-commits mailing list