Boost logo

Boost :

Subject: Re: [boost] [utility] Any interest for a macro which automatically creates default move-semantics, comparisson etc for a class
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2013-11-07 04:52:38


2013/11/6 Viktor Sehr <viktor.sehr_at_[hidden]>

> I've created a macro which automatically creates:
> - move constructor\assignment
> - swap member function
> - operator== and operator!=
> - operator<
> - assignment operator without side-effects if it throws.
>
> Example usage:
> class Foo {
> public:
> Foo() {}
> DEFAULT_EVERYTHING(Foo,
> ( (int)(a) )
> ( (std::vector<int>)(b) )
> ( (float)(c) )
> );
> };
>
> Any interest, suggestions or ideas?
> Ofcourse it might be split into several macros depending on what operators
> the class requests (if it's a move-only class etc), and perhaps an extra
> parameter enable if a member is up for sorting and comparison.
>
> Code available here:
> https://www.dropbox.com/s/dhkxj94rrh83az0/DefaultEverything.h
>

Looks pretty interesting, however there is a room for improvement:

* boost::swap is more optimal than ::std::swap (or just allow ADL during
swap)
* std::move vs boost::move?
* marking generated functions with BOOST_NOEXCEPT when possible. That's the
place when DEFAULT_EVERYTHING macro could be really helpful. In your
example all the types in Foo have noexcept moves and swaps, so at least all
moves and swap of Foo must be also marked with noexcept

-- 
Best regards,
Antony Polukhin

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk