What's so hard about constexpr allocation?
C++20 allows allocations during constant evaluation, but they must be deallocated in the same context, limiting constructs like `constexpr std::vector`. Challenges include constant destruction and access problems, requiring clearer rules.
Read original articleC++20 introduced the ability to perform allocations during constant evaluation, a significant change from previous standards where such allocations were not permitted. However, this capability is limited; any allocation must be deallocated within the same constant evaluation context. This limitation prevents the declaration of `constexpr std::vector` and similar constructs, which remains an issue in C++23 and may persist into C++26. The challenges are twofold: the constant destruction problem, which concerns ensuring that allocations can be safely deallocated, and the constant access problem, which deals with determining when the contents of an allocation can be treated as constant expressions.
The constant destruction problem arises when an allocation persists beyond its intended scope, leading to potential runtime errors if destructors are invoked on memory allocated during compile time. The constant access problem highlights the risk of accessing mutable data through pointers that are declared as constant, which can yield inconsistent results during execution.
To address these issues, the language must establish rules that prevent unsafe allocations while allowing useful constructs like `std::vector`. This involves distinguishing between different types of allocations, such as transient and non-transient allocations, and ensuring that mutable access does not compromise the integrity of constant expressions. The complexity of these problems suggests that a comprehensive solution is still needed to fully leverage `constexpr` allocations in C++.
Related
How the STL Uses Explicit
The WG21 meeting in St. Louis discusses a paper on using the `explicit` keyword in C++ proposals to establish a style guide for consistency. Guidelines differ between industry and Standard Library practices.
A Type for Overload Set
The article explores C++ overload set challenges, discussing issues with standard functions encountering problems due to overloading. It introduces proposal P3312 for a unique type to address these limitations, emphasizing the need for a more efficient solution.
Atomicless Per-Core Concurrency
The article explores atomicless concurrency for efficient allocator design, transitioning from per-thread to per-CPU structures on Linux. It details implementing CPU-local data structures using restartable sequences and rseq syscall, addressing challenges in Rust.
Malloc() and free() are a bad API (2022)
The post delves into malloc() and free() limitations in C, proposing a new interface with allocate(), deallocate(), and try_expand(). It discusses C++ improvements and emphasizes the significance of a robust API.
New Features in C++26
The ISO released new C++ standards on a three-year cycle, with C++26 proposals open until January 2025. Updates include hazard pointers, user-space RCU support, debugging headers, and template enhancements for improved functionality.
- Constexpr objects must be fully const (i.e. they cannot be modified, and non-const methods are not allowed to be called).
- Constexpr objects cannot allow access to mutable data (directly or indirectly). Maybe this is where the devil is in the details? Not sure... It seems that pointers to mutable via constexpr would be a niche thing anyway.
- Constexpr objects always exist for the duration of the program.
- Any allocations made during construction are done via a "const pool" allocator that no-ops deallocation, so that nothing blows up at shutdown. If everything is const/constexpr, the total allocation size should be decidable at compile time, I think?
It's useful to ensure that some things get calculated at compile time even if all eventuall access is at runtime.
Related
How the STL Uses Explicit
The WG21 meeting in St. Louis discusses a paper on using the `explicit` keyword in C++ proposals to establish a style guide for consistency. Guidelines differ between industry and Standard Library practices.
A Type for Overload Set
The article explores C++ overload set challenges, discussing issues with standard functions encountering problems due to overloading. It introduces proposal P3312 for a unique type to address these limitations, emphasizing the need for a more efficient solution.
Atomicless Per-Core Concurrency
The article explores atomicless concurrency for efficient allocator design, transitioning from per-thread to per-CPU structures on Linux. It details implementing CPU-local data structures using restartable sequences and rseq syscall, addressing challenges in Rust.
Malloc() and free() are a bad API (2022)
The post delves into malloc() and free() limitations in C, proposing a new interface with allocate(), deallocate(), and try_expand(). It discusses C++ improvements and emphasizes the significance of a robust API.
New Features in C++26
The ISO released new C++ standards on a three-year cycle, with C++26 proposals open until January 2025. Updates include hazard pointers, user-space RCU support, debugging headers, and template enhancements for improved functionality.