For this simple circular queue, I want to support C++11's move semantics so the push() doesnt incur a memory copy if it doesnt need to.
Circular<struct r, 1024> queue_;
queue_.push( { r0, r1 } );
However I'm concerned my implementation, specifically the second push() below, is unnecessary or too wordy.
template<typename T, size_t capacity>
class Circular
{
public:
Circular()
: have_(capacity)
, w_(0)
, r_(0)
{}
~Circular() {}
void push( const T& x ) { have_[ w_++ % capacity ] = x; }
void push( T&& x ) { have_[ w_++ % capacity ] = x; }
...
protected:
std::vector<T> have_;
size_t w_;
size_t r_;
};
Is there a better way to get these semantics? (Edit: are the intended semantics in fact implemented?)
Followup: Can we avoid repeating the body of the push() method while preserving the intended behavior?