Andrew Tomazos wrote:
> Suppose I have a C interface as follows:
>
> // create a new FOO
> FOO* foo_open(int params);
>
> // use FOO one or more times
> void foo_bar(FOO*);
>
> // destroy FOO
> foo_close(FOO*);
>
> and I want to wrap that in a C++11 class.
>
> class Foo
> {
> Foo(int params) : p(foo_open(params)) {}
>
> void bar() { foo_bar(p); }
>
> ~Foo() { if (p) { foo_close(p); } }
>
> private:
> FOO* p;
>
> // Clearly Foo cannot be copied otherwise you will call foo_close
> twice for the same handle
> Foo(const Foo&) = delete;
> Foo& operator= (const Foo&) = delete;
>
> // But we should be able to move it
> Foo(Foo&& that)
> {
> ???
> }
>
Foo(Foo&& that)

(that.p){that.p=0;}
> Foo& operator= (Foo&& that)
> {
> ???
std::swap(p,that.p);
OR
foo_close(p); p=that.p; that.p=0;
(it could be different if foo_close can throw)
> }
> };
>
> I am not clear how to implement the move constructor and move
> assignment. ?
Just try to consider what the destructor can do after you moved from
an object. Move assignment is often a synonym for swap (avoids
thinking too long) unless you want to be sure to destruct immediatly.
> What role does std::move play if any in this case?
Foo a(42);
Foo b=std::move(a);
a=std::move(b);
You may want to take a look at unique_ptr.