Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Created March 7, 2014 14:41
Show Gist options
  • Save derrickturk/9412661 to your computer and use it in GitHub Desktop.
Save derrickturk/9412661 to your computer and use it in GitHub Desktop.
Sometimes (*cough* linking GNU new (nothrow) still brings in a bunch of exception-handling crap *cough*) you just need an easy malloc'd buffer with automatic lifetime management.
template<class T>
using auto_buf = std::unique_ptr<T[], void(&)(void*) noexcept>;
template<class T>
auto_buf<T> make_auto_buf(std::size_t n) noexcept
{
return auto_buf<T>(static_cast<T*>(std::malloc(n * sizeof(T))), std::free);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment