Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12403 > unrolled thread
| Started by | Andrew Tomazos <andrew@tomazos.com> |
|---|---|
| First post | 2011-12-12 04:11 -0800 |
| Last post | 2011-12-12 13:45 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.c++
Writing move constructors and move assignment Andrew Tomazos <andrew@tomazos.com> - 2011-12-12 04:11 -0800
Re: Writing move constructors and move assignment Marc <marc.glisse@gmail.com> - 2011-12-12 13:15 +0000
Re: Writing move constructors and move assignment Nobody <nobody@nowhere.com> - 2011-12-12 13:45 +0000
| From | Andrew Tomazos <andrew@tomazos.com> |
|---|---|
| Date | 2011-12-12 04:11 -0800 |
| Subject | Writing move constructors and move assignment |
| Message-ID | <3e5a9cb8-013e-41ea-8ba8-5f1133ebf453@c13g2000vbh.googlegroups.com> |
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& operator= (Foo&&)
{
???
}
};
I am not clear how to implement the move constructor and move
assignment. ? What role does std::move play if any in this case?
-Andrew.
[toc] | [next] | [standalone]
| From | Marc <marc.glisse@gmail.com> |
|---|---|
| Date | 2011-12-12 13:15 +0000 |
| Message-ID | <jc4uq7$lhi$1@news-rocq.inria.fr> |
| In reply to | #12403 |
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):p(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.
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2011-12-12 13:45 +0000 |
| Message-ID | <pan.2011.12.12.13.45.16.498000@nowhere.com> |
| In reply to | #12403 |
On Mon, 12 Dec 2011 04:11:10 -0800, Andrew Tomazos wrote:
> Foo(Foo&& that)
> {
> ???
p = that.p;
that.p = NULL;
> }
>
> Foo& operator= (Foo&&)
Foo& operator= (Foo&& that)
> {
> ???
if (this != &that) {
p = that.p;
that.p = NULL;
}
return *this
> }
> };
> What role does std::move play if any in this case?
You can re-write the move constructor using the move assignment
operator and std::move as:
Foo(Foo&& that)
{
*this = std::move(that);
}
std::move just lets you get an rvalue reference for an lvalue, to cause
the correct overload to be used.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web