I have the following simple class:
class Source
{
public:
Source() = default;
Source(Source const&) = delete;
Source(Source&&) = defa
I realise this answer is a little bit late but to give an unmoveable class move sematics you could write a very simple wrapper class. For example:
#include <memory>
template <typename T>
class swap_wrapper
{
//! internal buffer to hold object (on heap)
std::unique_ptr<T> p_obj;
public:
//! allows any of objects constructors to be called directly
template <typename... Args>
explicit swap_wrapper(Args&&... args)
: p_obj(
new T( std::forward<Args>(args)... )
) { }
//! swaps pointer value of T object is unchanged therefore this
//! function puts no requirement on base class.
//! note p_obj is left as a nullptr so dereferencing will cause
//! undefined behaviour.
swap_wrapper (swap_wrapper &&other) noexcept
: p_obj(nullptr)
{
swap(other);
}
//! swaps pointer like above,
//! T object is not touched; just the pointer.
swap_wrapper &operator = (swap_wrapper &&other) noexcept
{
swap(other);
return *this;
}
//! uses swap member function of std:unique_ptr
void swap(swap_wrapper &other) noexcept
{
std::swap(p_obj, other.p_obj);
}
//! operators to allow access to stream
T& operator *() { return *p_obj; }
T const& operator *() const { return *p_obj; }
T * operator ->() { return p_obj.get(); }
T const * operator ->() const { return p_obj.get(); }
};
//! overload of default swap (calls member function swap)
template <typename S>
inline void swap(swap_wrapper<S> &one, swap_wrapper<S> &two) noexcept
{ one.swap(two); }
This wrapper can then be returned from functions, passed as an rvalue parameter, etc..
It turns out that the standard library implementation of GCC has not implemented yet the move and swap operation for the stream classes. See here for detail about the current status of C++11 features in the gcc standard library.
Thanks Jesse Good for giving the information and link.