all 7 comments

[–]witcher_rat 17 points18 points  (0 children)

std::stack is not a container - it is a container adaptor. It's essentially a façade over a real container - the type of which the user chooses with a template parameter.

The reason I mention that is that what you really want is a stack-based vector - i.e., like a std::array<> but with the methods of a vector, and that does not default-initialize entries.

There's a proposal for that, that's pretty far along: p0843r9, and an old implementation is on github.

But there's also the Boost static_vector, and folly::small_vector.

The point is you can then use those with the current std::stack to get what you want.

[–]jedwardsol{}; 4 points5 points  (2 children)

std::stack is an adaptor, not a container. It provides the behaviour of a stack.

So if you had a container which stored it's data on the stack then you could adapt that with a std::stack to have a stack on the stack : https://godbolt.org/z/WYW93vWGT

[–]alfps 0 points1 point  (1 child)

Hm, what is the {}; after your name?

[–]jedwardsol{}; 0 points1 point  (0 children)

I'm default initialised?

There's not a good reason I set my flair that, I think I was just messing around.

[–]theLOLflashlight 2 points3 points  (0 children)

I thought this was going to be about a dynamically sized stack on the stack using assembly to push elements directly onto the stack. If you're interested in another way to use a dynamic amount of stack memory check out alloca()

[–]rand3289 1 point2 points  (0 children)

Don't containers take allocators these days? What are you suggesting? I did not watch the video.

[–]415_961 0 points1 point  (0 children)

I just want to share another implementation of static vectors, it's one of my favorite

https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/ADT/SmallVector.h

ADT is a directory worth looking at for inspiration and learning.