1. Problem It Solves
C++23 exposes three previously awkward areas: starting implicit-lifetime objects in raw storage, requesting allocation with an actual returned count, and using more smart-pointer operations during constant evaluation. The goal is to make the relevant rule visible in code instead of relying on an assumption about what “C++23 support” means.
2. Prerequisites
Day 6: RAII and memory lifetime.
Day 8: expanded constant evaluation.
3. Core Idea
Storage is an empty room, lifetime is permission to treat bytes as an object, and ownership is the key. allocate_at_least may rent a larger room and reports its true size. Read the syntax from left to right, identify the value, object, or range being transformed, and then check its resulting type, lifetime, or ownership. Standardization and implementation are separate: a C++23 mode may still lack one library component, so a feature-test macro is part of responsible portable use.
4. Minimal Syntax
auto block = allocator.allocate_at_least(count);5. How It Works
The sample builds the smallest expression or object needed for Lifetime Start, Allocation, and
constexprSmart Pointers.It checks constexpr smart-pointer work and conditionally demonstrates allocation or explicit lifetime start. The compiler and library apply the relevant rule before the program observes the result.
The program prints or checks a fixed value plus accurate support information for lower-level facilities, making the important behavior easy to verify.
6. Common Mistakes
Accessing storage before an object's lifetime begins is undefined behavior; deallocating with the requested count instead of the returned count can violate the allocator contract.
Enabling C++23 mode without checking the relevant feature macro may select code that the installed compiler or standard library does not implement yet.
7. When to Use It
Use it when the task involves allocator implementations, object pools, and compile-time ownership experiments with strict lifetime reasoning.
Avoid it when the task involves ordinary application objects that can be constructed normally with containers or smart-pointer factories.
8. Simple Example
A pool asks for at least eight slots, records the returned capacity, and starts objects only in selected raw slots. The downloadable program keeps the data fixed so the output can be compared without entering input.
Complete sample code
Source file
cpp23/33_lifetime_allocation_constexpr_smart_pointers/main.cpp
#include <cstddef>
#include <iostream>
#include <memory>
#include <version>
int main() {
#if defined(__cpp_lib_constexpr_memory) && \
__cpp_lib_constexpr_memory >= 202202L
constexpr int owned = [] {
auto value = std::make_unique<int>(7);
return *value;
}();
static_assert(owned == 7);
std::cout << "constexpr owner=" << owned << '\n';
#else
std::cout << "constexpr smart pointers unavailable\n";
#endif
#if defined(__cpp_lib_allocate_at_least)
std::allocator<int> allocator;
auto block = allocator.allocate_at_least(2);
std::construct_at(block.ptr, 9);
std::cout << "allocated value=" << *block.ptr << '\n';
std::destroy_at(block.ptr);
allocator.deallocate(block.ptr, block.count);
#else
std::cout << "allocate_at_least unavailable\n";
#endif
#if defined(__cpp_lib_start_lifetime_as)
alignas(int) std::byte storage[sizeof(int)]{};
int* value = std::start_lifetime_as<int>(storage);
*value = 11;
std::cout << "started value=" << *value << '\n';
#else
std::cout << "start_lifetime_as unavailable\n";
#endif
}
9. Key Takeaways
Separate the language rule from compiler and library availability.
Keep lifetime, ownership, and deduced types visible when they affect correctness.
Prefer the smallest syntax that communicates the intent.
Guard facilities that are not yet uniformly implemented.
10. Self-Check Questions
Easy — What is the smallest syntax in Section 4, and what main job does it perform?
Medium — Read the sample program: which value, type, or branch is observed, and why?
Hard — Why are obtaining suitably aligned storage and starting an object's lifetime separate requirements, and which operations satisfy each one?