brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · fdde552 Raw
32 lines · plain
1.. title:: clang-tidy - portability-std-allocator-const2 3portability-std-allocator-const4===============================5 6Report use of ``std::vector<const T>`` (and similar containers of const7elements). These are not allowed in standard C++, and should usually be8``std::vector<T>`` instead."9 10Per C++ ``[allocator.requirements.general]``: "T is any cv-unqualified object11type", ``std::allocator<const T>`` is undefined. Many standard containers use12``std::allocator`` by default and therefore their ``const T`` instantiations13are undefined.14 15libc++ defines ``std::allocator<const T>`` as an extension which will be16removed in the future.17 18libstdc++ and MSVC do not support ``std::allocator<const T>``:19 20.. code:: c++21 22  // libstdc++ has a better diagnostic since https://gcc.gnu.org/bugzilla/show_bug.cgi?id=4810123  std::deque<const int> deque; // error: static assertion failed: std::deque must have a non-const, non-volatile value_type24  std::set<const int> set; // error: static assertion failed: std::set must have a non-const, non-volatile value_type25  std::vector<int* const> vector; // error: static assertion failed: std::vector must have a non-const, non-volatile value_type26 27  // MSVC28  // error C2338: static_assert failed: 'The C++ Standard forbids containers of const elements because allocator<const T> is ill-formed.'29 30Code bases only compiled with libc++ may accrue such undefined usage. This31check finds such code and prevents backsliding while clean-up is ongoing.32