News for hackers. Stuff that matters.
Re:C++14 and SDL2: Managing Resources by simias on Friday April 18, 2014 @01:40PM (#7610244)
Wouldn't it be simpler to just wrap the SDL_Window C object in a C++ class? That seems like a lot of boilerplate to just save oneself a destructor call.

Also, I'm not usually one to bash C++ for its syntax (I've written quite a lot of C++ in my days and still use it from time to time) but my god I find that template spaghetti code physically painful to read.

Re:C++14 and SDL2: Managing Resources by plorkyeran on Friday April 18, 2014 @01:43PM (#7610272)
The point of this is you can write it once and use it for every type rather than having to write a wrapper for each one.
Re:C++14 and SDL2: Managing Resources by simias on Friday April 18, 2014 @01:50PM (#7610306)
If I understand correctly you still have to write something like

    template<typename... Arguments> auto make_window(Arguments&&... args)
    {
        return detail::make_resource(SDL_CreateWindow, SDL_DestroyWindow, std::forward<Arguments>(args)...);
    }
for each resource.

Quite honestly at that point I'd sooner use a good old variadic macro, C99 style. I mean, sure, it's arguably less "elegant" and C++-ish but it'll be easier to understand, to maintain and won't spam me with pages of arcane template errors when I forget a colon somewhere, so it'll be easier to debug as well.

That's the double edged sword of the C++ template system I suppose, in order to gain in purity they lost the simplicity of the C macro system without gaining the expressiveness and convenience of the Lisp macro system. In the end, what percentage of C++ coders use templates outside of the STL you think?

Re:C++14 and SDL2: Managing Resources by pja on Friday April 18, 2014 @02:01PM (#7610373)
The C++ template will give you extra type checking that the varadic macro won't, which is worth the tradeoff for the obscure error messages for me.

(At least the errors have got slightly better in recent g++ releases with colourised error output.)

Re:C++14 and SDL2: Managing Resources by masklinn on Friday April 18, 2014 @02:16PM (#7610464)
> If I understand correctly you still have to write something like

You don't have to, you can create further helper function calling the generic thing but it's optional.

Re:C++14 and SDL2: Managing Resources by devcodex on Friday April 18, 2014 @11:49PM (#7612526)
The code that makes use of templates and such is meant to be library code in the same way that STL is, not something the average user interacts with other than to know they need to pass an Init and Destroy function followed by initialization parameters. I don't see people tearing apart <tuple>'s code because of its complexity, or <vector> or any other reusable STL component that most developers who've complained about the verbosity of my solution use every day without batting an eye.

In these proposed solutions I've not yet seen one that has the capability to do this:

    auto window = make_resource(SDL_CreateWindow, SDL_DestroyWindow, ....);
With my solution all you have to do is call make resource with different parameters it automatically works with other C style init/destroy created resources, not just from the SDL library but any C library that follows that practice.

Can you try posting what your solution to the general problem would look like?

Re:C++14 and SDL2: Managing Resources by angersock on Friday April 18, 2014 @03:00PM (#7610689)
The thing is, you're going to be wrapping in a class anyways, so why not move all the nonsense into a normal destructor and then use normal smart pointers?

I suggest that this is somewhat solving the wrong problem.

Re:C++14 and SDL2: Managing Resources by 10098 on Friday April 18, 2014 @01:49PM (#7610302)
This, and also it would bother me a lot to know that I'm essentially re-implementing what is already available in the standard library.