• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

10 replies to this topic

### #1Prune  Members

Posted 25 November 2013 - 06:50 PM

POPULAR

#define RELOAD(X, Y, ...) { X.~Y(); new (&X) Y(__VA_ARGS__); }

"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

### #2Ectara  Members

Posted 25 November 2013 - 07:35 PM

That's... awful. Where would this get used?

### #3Nypyren  Members

Posted 25 November 2013 - 07:43 PM

Oh dear god, is that for object pooling?

### #4Mussi  GDNet+

Posted 25 November 2013 - 08:42 PM

What? I don't even...

Edited by Mussi, 25 November 2013 - 08:42 PM.

### #5ByteTroll  Members

Posted 26 November 2013 - 12:29 AM

That entire thing is ugly, but I think the worst part is where the destructor is explicitly called..and from a macro none-the-less

EDIT:

That's... awful. Where would this get used?

I hope nowhere!

Edited by ByteTroll, 26 November 2013 - 12:30 AM.

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob

### #6ultramailman  Prime Members

Posted 26 November 2013 - 07:22 PM

No do while(0)? No () around X?

Edited by ultramailman, 26 November 2013 - 07:23 PM.

### #7samoth  Members

Posted 27 November 2013 - 08:08 AM

The use of a macro and var args to initialize the new object kind of makes me shudder at first, but then again... since the constructor will make sure the parameters are correct, this macro (ugly as it is) is type-safe as well. The fact that Y is used to call the destructor implicitly prevents you from scrapping the object in favour of a different type of object which might have a bigger storage size, too. Actually that is quite ingenious (assuming it's intentional, not by coincidence).

Other than being a macro (and thus ugly, and non-obvious), I can actually find very little to complain about. What the macro does is weird, but perfectly legal from what I can tell.

Let's just hope that nobody ever calls this with a const object (which would invoke undefined behavior according to §3.8/9).

### #8Juliean  Members

Posted 27 November 2013 - 08:27 AM

Now that must be the ancestor of the move-assignement operator

Foo class(...);
class = std::move(Foo(...)); // pretty much the same as the RELOAD-macro, if operator=(Foo&&) is implemented

### #9samoth  Members

Posted 27 November 2013 - 08:49 AM

Now that must be the ancestor of the move-assignement operator

Foo class(...);
class = std::move(Foo(...)); // pretty much the same as the RELOAD-macro, if operator=(Foo&&) is implemented

Somewhat similar, but I would say it's rather the opposite. Moving an object means "stealing" a temporary object (without anyone noticing) and assigning that same nameless object to a name.

The above macro explicitly ends the lifetime of a named object, then steals its storage, reconstructs another object in-place, and implicitly reassigns it to the same name.

For entertainment, I've templatized it:

#include <new>
#include <type_traits>
template<typename T, typename... V> void reuse_inplace(T& obj, V... args)
{
static_assert(!std::is_const<T>::value, "in-place construction over a const object");
obj.~T(); new(&obj) T(args...);
};


Not like it's much better, but at least it isn't a macro now, and it can't steal a const object

Edited by samoth, 27 November 2013 - 08:49 AM.

### #10Prune  Members

Posted 29 November 2013 - 05:23 PM

The fact that Y is used to call the destructor implicitly prevents you from scrapping the object in favour of a different type of object which might have a bigger storage size, too. Actually that is quite ingenious (assuming it's intentional, not by coincidence).

It was intentional, but what I didn't realize was that Y is unnecessary as a parameter:

#define RELOAD(X, ...) { typedef decltype(X) Y; X.~Y(); new (&X) Y(__VA_ARGS__); }

"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

### #11Prune  Members

Posted 29 November 2013 - 05:28 PM

#include <new>
#include <type_traits>
template<typename T, typename... V> void reuse_inplace(T& obj, V... args)
{
static_assert(!std::is_const<T>::value, "in-place construction over a const object");
obj.~T(); new(&obj) T(args...);
};


Nice. I'll switch to this once I move to VC++2013, as 2012 doesn't support variadic templates.

"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)