Jason Turner provides a clear and practical look at how `inplace_vector` finally brings stack-allocated flexibility and `constexpr` power to the C++ standard library. It is a vital update for developers seeking to eliminate heap overhead without losing the familiar vector interface.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
C++ Weekly - Ep 542 - C++26's inplace_vector
Added:Hello and welcome to C++ Weekly. I'm your host, Jason Turner, available for code reviews, contracting, on-site training. Contact me if you have any interest in these things.
In this episode, I want to talk about inplace_vector for C++26, and thankfully, it is 'constexpr'. Before I continue, I want to remind you that I've got upcoming workshops at NDC TechTown and CppCon coming in September.
All right, inplace_ vector. Now, the point of this thing is basically to have an...resizable array; no, that's not a good way to put it; a vector that lives on the stack, let's call it that.
I have implemented something like this at least a half dozen times in different contexts for different reasons, largely because I wanted vector-like capabilities with 'constexpr', and this is 'constexpr', so I don't have to do it anymore. Let's see if it worked.
This week's episode is sponsored by HRT. "If you've attended any C++ conferences this year, there's a good chance you've already heard of HRT as they have sponsored several conferences.
HRT is known for its deep engineering culture and strong focus on performance, and they are looking to hire the sharpest engineers to help them to develop and optimize the technology at the core of their business. They're headquartered in New York, but they have offices in Austin, Chicago, Boulder, London, Dublin, and across Asia. They're looking primarily for C++ and Python developers. I know since you're a viewer of C++ Weekly, you are a C++ programmer and you probably already program in Python, too. So, go ahead and check out the link on the screen for openings that they have. And remember, you don't need a financial background, just strong engineering fundamentals, and they'll teach you the rest of what you need to know."
We're off to a good start. It compiles when including the header.
All right, I've got an inplace_vector of ints. "No member named 'inplace_vector'." Make sure the compiler is in C++26 mode... Here we go. We have optimizations enabled. We are on Clang trunk, and I think it is safe to argue that Clang trunk has a little bit of a regression here in removing these unused statements because all it's doing is setting up stack space and doing nothing with it.
Let's push_back some sort of value into this thing. This should behave just like a vector except the thing is always on the stack. I am almost at my capacity. Let's add something else.
Now I have reached my capacity and I should be able to do "return of data.size." This should show 4. Let's do this. "Data.capacity - data.size," and out of curiosity, let's switch to GCC and see if this can compile just as well. It can. I'm still kind of surprised that it didn't optimize all of this away, honestly, because it's all on the stack.
Let's initialize the thing and see if we can do list initialization with it. I can do list initialization and I see that 0 is being returned. Let's remove that. Let's take a quick look at the header file. We have a constructor that takes potentially a range, potentially initializer_list.
Its destructor is defined as being trivial if the things that it's containing is trivial. Um, yeah, it's trivially MoveConstructable, it's trivially CopyConstructable. So this should be good. Assign range, assign... It looks and feels like everything that you would expect from a C++ vector. So let's overflow the range and see what happens to this thing. "Allocate_exception bad_alloc." Okay, so we're going to get an actual exception thrown from this thing saying that we can't, uh, overflow the capacity. The capacity can't grow, so it's an out of memory error.
Interesting. Let's, out of curiosity, put this into a 'consteval' block like we were just playing with and see if the latest version of GCC supports this. It does, and I get "uncaught exception of type 'bad_alloc'." This is a compile time error that there's an uncaught exception. I am allowed to throw and catch at compile time in C++26.
All right. So, I just swallowed the exception here at compile time.
I have no idea if this is going to do what I want it to do. "Request for member 'size' in 'what' is of non-class." Okay, so it's a const character pointer, I can't just use that. What happens if I say, "No, it's okay, really, I want a string_view," which should be compatible. Yeah, okay, I can see that. All right, so that's not going to work. Um well, wonder what happens if I do that.
"Static assertion failed." Yes, of course. But if that block does not get evaluated... static assertion still failed. Okay, I kind of expected that as well. All right. Well, anyhow, you can see that we now have something that lives on the stack, behaves exactly like a vector, and can actually do things like throw and catch exceptions in C++26 mode, and then there's this whole 'consteval' block thing. There's a lot coming in C++26. I should probably put together, like, a book or something on that at some point. I don't know. Thanks for watching this episode of C++ Weekly. Look out for inplace_vector. Pay attention to the fact that you can throw and catch exceptions at compile time with C++26 and I'll catch you in the next episode.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

WOW! Judge TURNS THE TABLES on Trump in His OWN $10B LAWSUIT!!!
MeidasTouch
197K views•2026-07-23

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Steam and Xbox Just Dropped The Hammer On PlayStation
OhNoItsAlexx
9K views•2026-07-23

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23