Skip to content

Instantly share code, notes, and snippets.

@saxbophone
Created September 16, 2023 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saxbophone/fcdc2679bc3dcffbde95e5ddde403091 to your computer and use it in GitHub Desktop.
Save saxbophone/fcdc2679bc3dcffbde95e5ddde403091 to your computer and use it in GitHub Desktop.
using std::span to babysit array-new-allocated data
#include <cstddef>
#include <span>
template <typename T>
constexpr std::span<T> allocate(std::size_t size) {
return {new T[size], size};
}
template <typename T>
constexpr void deällocate(std::span<T>& handle) {
delete[] handle.data();
handle = {};
}
#include <cassert>
#include <iostream>
int main() {
auto memory = allocate<int>(55'000);
assert(not memory.empty());
deällocate(memory);
assert(memory.empty());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment