0

I'm a little unclear if I could have an object that has uninitialized values that are not accessible.

E.g.: Say like a stack based on an c-array. If I don't have any elements in the stack, then the elements stored could be uninitialized, but when I push an element onto the stack, I would use placement new to use the copy constructor and on pop, I would placement delete.

Actual access to the elements beyond the head of the stack would not be accessible, or accessing them would be UB (just like elements beyond the actual c-array).

Adrian
  • 10,246
  • 4
  • 44
  • 110
  • 1
    You'll need to move on to C++20 for that. – StoryTeller - Unslander Monica Dec 04 '22 at 22:04
  • Thx @StoryTeller-UnslanderMonica. Just verifying. – Adrian Dec 04 '22 at 22:06
  • 2
    No placement new in constexpr anyways. You'll need [`std::construct_at`](https://en.cppreference.com/w/cpp/memory/construct_at) from C++20 – Artyer Dec 04 '22 at 22:11
  • That's interesting @Artyer. Why did they do that rather than put a `constexpr` on the placement new? I'm going to have to look into this further. Thx for the info. – Adrian Dec 04 '22 at 22:30
  • 1
    @Adrian This is a good explanation for why: https://stackoverflow.com/a/74035713 (TLDR: some existing implementations would have difficulty using placement new during constant evaluation, it's easier to just make a separate function) – Artyer Dec 05 '22 at 00:04

0 Answers0