0

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 malloced memory, which should be freed rather than deleted.

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).

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142

0 Answers0