Jump to content

  • Log In with Google      Sign In   
  • Create Account

Reload

  • You cannot reply to this topic
10 replies to this topic

#1 Prune   Members   -  Reputation: 218

Like
5Likes
Like

Posted 25 November 2013 - 06:50 PM

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

Sponsor:

#2 Ectara   Crossbones+   -  Reputation: 2978

Like
3Likes
Like

Posted 25 November 2013 - 07:35 PM

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



#3 Nypyren   Crossbones+   -  Reputation: 4345

Like
0Likes
Like

Posted 25 November 2013 - 07:43 PM

Oh dear god, is that for object pooling?



#4 Mussi   Crossbones+   -  Reputation: 1976

Like
0Likes
Like

Posted 25 November 2013 - 08:42 PM

What? I don't even...


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


#5 ByteTroll   Crossbones+   -  Reputation: 1399

Like
0Likes
Like

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.

▬▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬▬▬▬▬▬
I see the future in 1's and 0's
▬▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬▬▬▬▬▬

"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

#6 ultramailman   Prime Members   -  Reputation: 1571

Like
0Likes
Like

Posted 26 November 2013 - 07:22 PM

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


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


#7 samoth   Crossbones+   -  Reputation: 4791

Like
0Likes
Like

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



#8 Juliean   GDNet+   -  Reputation: 2651

Like
0Likes
Like

Posted 27 November 2013 - 08:27 AM

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

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


#9 samoth   Crossbones+   -  Reputation: 4791

Like
1Likes
Like

Posted 27 November 2013 - 08:49 AM

 

Now that must be the ancestor of the move-assignement operator biggrin.png

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  smile.png


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


#10 Prune   Members   -  Reputation: 218

Like
0Likes
Like

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)

#11 Prune   Members   -  Reputation: 218

Like
0Likes
Like

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)





PARTNERS