C++26:弃用或移除库特性
C++26: Deprecating or removing library features

原始链接: https://www.sandordargo.com/blog/2025/03/19/cpp26-deprecate-remove-library-features

C++26移除了几个已弃用的库特性。`std::allocator` 有问题的typedef和`std::basic_string::reserve()`的无参数重载已被移除。不安全的``头文件和`std::strtok`(来自独立C++)也被移除。自C++98起就已弃用的`char*`流最终被移除,并被现代替代方案取代。已弃用的`std::shared_ptr`原子访问API也被移除,替换为类型安全的`std::atomic>`。最后,由于规范问题,`std::wstring_convert`和相关函数也被移除。 C++26也弃用了一些特性。`std::is_trivial`和`std::is_trivial_v`被弃用,取而代之的是具体的`is_trivially_XXX`检查。此外,由于其规范不令人满意且缺乏一致的实现,`std::memory_order::consume`也被弃用。

Hacker News 最新 | 往期 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 C++26:弃用或移除库特性 (sandordargo.com) pjmlp 7小时前 13 分 | 隐藏 | 往期 | 收藏 | 2 条评论 zabzonk 6分钟前 | 下一条 [–] 我很惊讶竟然花了这么长时间才去除 `strtok()`。这是我见过的设计最糟糕、误用最多的函数之一——我遇到的几乎所有使用它的情况都是错误的。与其使用它,不如自己编写一个小型解析器。 回复 johnisgood 7分钟前 | 上一条 [–] `strtok` 从 C 语言中也移除了?该死。我想我还是要坚持使用 C99。 回复 加入我们,参加 6 月 16-17 日在旧金山举办的 AI 初创公司学校! 指导原则 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系我们 搜索:
相关文章
  • (评论) 2024-09-02
  • (评论) 2024-07-22
  • (评论) 2025-03-17
  • (评论) 2023-11-29
  • 延迟技术规范:时机已到 2025-03-19

  • 原文

    In the previous article, we discussed what language features are removed from C++26. In this one, we are going to cover both language features that are finally removed after a few years of deprecation, and also those that are getting deprecated by C++26.

    As a reminder, a removal from the language usually happens in two steps. First a feature gets deprecated, meaning that its users would face compiler warnings for using deprecated features. As a next step, which in some cases never comes, the compiler support is finally removed.

    Remove Deprecated std::allocator Typedef From C++26

    The std::allocator has a typedef that was deprecated in C++20 and now finally it’s removed. Though it’s a minor corner case, but it’s been so easy to misuse it that it was rather embarrassing for the committee, hence the removal by P2868R3.

    Classes deriving from std::allocator don’t synthesise the typedef member correctly and the allocator authors have to add their own typedef to ensure correct behaviour. If they knew about it…

    Removing function overload of std::basic_string::reserve() that takes no

    I just learned from P2870R3 that std::basic_string::reserve had an overload taking no arguments. As it was a poor substitute for std::basic_string::shrink_to_fit, it was deprecated in C++20.

    Now it’s gone.

    reserve used to have a default value of 0 for its sole argument turning it into a non-binding shrink_to_fit. But shrink_to_fit was introduced as an independent function in C++11 and as such this behaviour of reserve became superfluous and a “few” years later it got deprecated.

    If your code uses reserve() without any arguments, migration is simple, just replace it it with shrink_to_fit.

    Remove Deprecated Unicode Conversion Facets from C++26

    The <codecvt> header was first provided by C++11 and then deprecated by C++17 due to its underspecification, notably a lack of error handling. It’s removed in C++26 by P2871R3.

    This library contained several helper classes to convert between different UTF formats. Due to the bad specification, ill-formed UTF strings could be used as an attack vector.

    This change is about improving language safety.

    Freestanding: removing std::strtok

    std::strtok has been part of C++ freestanding, in other words, it’s a C function and was part of C++ to help with compatibility. As std::strtok has been removed from C2X standards, C++ doesn’t need it anymore either.

    “A freestanding C++ implementation is mostly a superset of a freestanding C implementation, even in the “C” parts of C++. This means that a freestanding C++ implementation can not generally be built on top of a minimal freestanding C implementation. Either the C++ implementation must provide some of the C parts, or the C++ implementation will require a C implementation that provides more than the minimum.” - source

    Removing deprecated strstreams

    C++20 introduced the ability to move strings efficiently out of stringsteams and C++23 brought us the spanstream library that we already covered in C++23: The rise of new streams.

    Given that C++ now had superior replacements for char* stream, now they are finally removed.

    Why finally?

    Well, apparently, char* streams have been the largest and oldest deprecated feature in the standard. They were marked for future deprecation and possible removal almost 30 years ago! Yes, we are talking about C++98.

    Will we need to undeprecate these features?

    Hopefully not.

    Removing deprecated std::shared_ptr Atomic Access APIs

    C++11 introduced atomics and smart pointers. Among others, it also introduced a free function API for atomic access to shared_ptr. It was an easy-to-use API so it was deprecated by C++20, along with the introduction of its type-safe replacement std::atomic<shared_ptr<T>>. C++26 removes the deprecated API thanks to the acceptance of P2869R4.

    While the old API expected that the shared object is not used directly, the API made it possible which led to undefined behaviour, typically producing a data race.

    To deal with the deprecation, one must perform two steps:

    • include the <atomic> header
    • replace shared_ptr<T> with std::atomic<shared_ptr<T>>

    Alternatively, one might also prefer using std::atomic member functions directly, instead of using std::shared_ptr overloads. For further details check out section 5.1 in P2869R4.

    Removing std::wstring_convert

    std::wstring_convert, std::wbuffer_convert and some related helper functions were introduced by C++11, deprecated with C++17 and finally removed in C++26 by P2872R3. Have you ever used it? I haven’t heard about it before.

    They help conversions between normal-sized and wide strings.

    The reason for their removals is that they are underspecified, there are a handful of open LWG issues related to them, and improving these facilities “would require more work than the committee wishes to invest to bring it up to the desired level of”.

    Deprecating std::is_trivial and std::is_trivial_v

    When I saw this accepted proposal I was surprised. Removing std::is_trivial? Aren’t people talking so much about trivial classes? But that’s simply a sign of my shallow knowledge.

    The fact is that people don’t talk about trivial classes - which notion is also deprecated. People talk about trivially default constructible, trivially copyable or trivially copy assignable classes. Often these properties are not needed at the same.

    In addition, is_trivial doesn’t even check that the required constructors are public.

    Use the appropriate is_trivially_XXX checks instead of the general is_trivial, especially now that the latter is deprecated by P3247R2

    Defang and deprecate std::memory_order::consume

    The problem of std::memory_oder::consume has been around for a decade. Its specification is not satisfactory, it’s difficult to implement and not even required by most widely-used CPU architectures. Given these problems, most academic work simply ignores its existence and uses other memory models.

    But even implementations mostly map it to std::memory_oder::acquire. As there is no will and consensus to improve it, the best next step is to deprecate it which P3475R1

    Conclusion

    After having discussed last week language features removed from C++26, this week we covered what library features are removed or started their way to be removed through their deprecation.

    Deprecation is not always a one-way road. Next week, we’ll cover a feature that is going to be underprecated in C++26. Stay tuned.

    Connect deeper

    If you liked this article, please

    联系我们 contact @ memedata.com