Created
September 16, 2023 13:31
-
-
Save saxbophone/fcdc2679bc3dcffbde95e5ddde403091 to your computer and use it in GitHub Desktop.
using std::span to babysit array-new-allocated data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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