C ++ 11を使用している場合は、次のようなものを使用してこれを行うことができます。
#include <functional>
#include <utility>
#include <type_traits>
struct noncopyable_but_still_moveable {
noncopyable_but_still_moveable(const noncopyable_but_still_moveable&) = delete;
noncopyable_but_still_moveable(noncopyable_but_still_moveable&&) = default;
noncopyable_but_still_moveable() = default;
noncopyable_but_still_moveable& operator=(const noncopyable_but_still_moveable&) = default;
noncopyable_but_still_moveable& operator=(noncopyable_but_still_moveable&&) = default;
};
template <typename T>
struct test : noncopyable_but_still_moveable {
test(T) {}
// the rest is irrelevant
};
template <typename T>
test<T> make_test(T&& val) {
return test<typename std::remove_reference<T>::type>(std::forward<T>(val));
}
int main() {
auto && w = make_test(0);
}
boost::noncopyable
コピーコンストラクターが削除された型に置き換えたことに注意してください。これは、コピー不可能なものを作成するC ++ 11の方法であり、ブーストクラスも移動できないために必要です。もちろん、それをクラス自体の中に入れて、それ以上継承しないようにすることもできます。
C ++ 11がない場合は、Boostmoveなどを使用してこれらのセマンティクスをエミュレートする必要があります。