This should get the the current working directory on Linux in RAII fashion:
#include <iostream>
#include <memory>
#include <unistd.h>
#include <cstdlib>
int main() {
using namespace std;
ios_base::sync_with_stdio(false);
//unique_ptr<const char> cwd (get_current_dir_name());
//unique_ptr<const char, [](const char* ptr){ free(ptr) }> cwd (get_current_dir_name());
unique_ptr<const char> cwd (get_current_dir_name());
cout<<cwd.get()<<'\n';
return 0;
}
It should'nt be using the default deleter because get_current_dir_name()
returns malloc
ed memory, which should be free
d rather than delete
d.
How can I get the custom deleter to work?
The commented-out lines are my failing attempts that won't compile
on gcc 4.9.2 (-std=c++14
).