Skip to content

Instantly share code, notes, and snippets.

@lethee
Created May 2, 2016 00:46
Show Gist options
  • Save lethee/b6752d1a2bd582b907d82d4e03ebec6b to your computer and use it in GitHub Desktop.
Save lethee/b6752d1a2bd582b907d82d4e03ebec6b to your computer and use it in GitHub Desktop.
C++11 shared_ptr, unique_ptr, and deleter
#include <memory>
#include <cstdio>
using namespace std;
int myfclose(FILE* stream)
{
printf("%s\n", __func__);
return fclose(stream);
}
int main(void)
{
//shared_ptr<FILE> fp(fopen("out.out.out", "w"), myfclose);
//unique_ptr<FILE, int(*)(FILE*)> fp(fopen("out.out.out", "w"), myfclose);
unique_ptr<FILE, decltype(&fclose)> fp(fopen("out.out.out", "w"), myfclose);
fprintf(fp.get(), "test\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment