News for hackers. Stuff that matters.
Re:C++14 and SDL2: Managing Resources by tdicola on Friday April 18, 2014 @03:31PM (#7610845)
This is a clever use of unique_ptr, but I can't help but think this is missing the forest through the trees. There's a lot of difficult to read code so you can use a C library resource with a bunch of C APIs. If you wrap the window in a class you can get simple RAII semantics, and add a friendly more C++ API.

What if you want to add event handlers for window events, change the window title, or even (gasp) throw exceptions instead of constantly check return codes? Wrapping the resource in a class gives you a great place to add this functionality. Without the class you have a nice resource that won't leak but still feels like you're programming C.

Re:C++14 and SDL2: Managing Resources by michaelrmmiller on Friday April 18, 2014 @03:50PM (#7610941)
If you were creating a C++ wrapper, you still might prefer to use this same technique. Make the unique_ptr-wrapped resource a member of the wrapper instead of the raw resource. That would give the wrapper the same semantics as unique_ptr without any extra work. If you wanted shared ownership, use shared_ptr instead. Lastly, you wouldn't need to manually declare a destructor to free the resource.

In the end, I find it more readable to see unique_ptr and shared_ptr than raw resources. They describe more than just resource management.