std::move for existing standard

J

Jonathan Lee

Hi all,
I want to begin using move semantics in my code assuming
only the current standard. I thought I could provide move
constructors and assignments by typedef-ing rvalue references
to "moveType" when C++0x is available, and otherwise using
a proxy "movable" class. Just to make the transition easier.
Example below.

Any thoughts?

--Jonathan


// begin: movetest.cpp
#include <cstddef>
#include <algorithm>

#if (__cplusplus > 199711L)
// Use the C++0x std::move if available
#include <utility>
namespace cpp0x = std;
#else
// Older versions of C++ use proxy class
namespace cpp0x {

template<class T>
class movable : public T {
public:
movable(T& t):T() { T::swap(t); }
};

template<class T>
movable<T> move(T& t) {
// rely on RVO
return movable<T>(t); }

}
#endif

class dummy {
char* ptr;
std::size_t len;
public:
#if (__cplusplus > 199711L) /* c++0x */
typedef dummy&& moveType;
#else
typedef cpp0x::movable<dummy>& moveType;
#endif

dummy(): ptr(0), len(0)
{ }
dummy(std::size_t n):ptr(n ? new char[n] : 0), len(n)
{ }
dummy(const dummy& d):ptr(d.len ? new char[d.len] : 0), len(d.len)
{ }
dummy(moveType d):ptr(d.ptr), len(d.len) {
d.ptr = 0, d.len = 0;
}

dummy& operator=(moveType d) {
d.swap(*this);
return *this;
}

void swap(dummy& d) {
std::swap(ptr, d.ptr);
std::swap(len, d.len);
}
};

int main () {
dummy a(5);
dummy b;

b = cpp0x::move(a);

return 0;
}
// end: movetest.cpp
 
A

Alf P. Steinbach /Usenet

* Jonathan Lee, on 29.06.2010 17:07:
Hi all,
I want to begin using move semantics in my code assuming
only the current standard. I thought I could provide move
constructors and assignments by typedef-ing rvalue references
to "moveType" when C++0x is available, and otherwise using
a proxy "movable" class. Just to make the transition easier.
Example below.

Any thoughts?

Without having looked at the code (snipped) you're definitely on shaky ground,
because rvalue references were introduced in C++0x after a great many attempts
to do what you're describing failed.

You might take a look at Andrei's old Mojo (DDJ article). It worked but at as I
recall the cost of probiting certain optimizations with actual compilers. As I
recall there was also an issue with MSVC.

I.e., this is like a perpetuum mobile: one doesn't need know exactly where the
reasoning goes wrong, in order to be able to say it won't work (or work well).


Cheers & hth.,

- Alf
 
J

Jonathan Lee

You might take a look at Andrei's old Mojo (DDJ article). It worked but at as I
recall the cost of probiting certain optimizations with actual compilers. As I
recall there was also an issue with MSVC.

I'll take a look, thanks. Actually, I think I've seen some earlier
attempts before that relied on trying to "detect" temporaries in
various ways. I sorta thought that since the std::move(lvalue)
notation was now agreed upon, it would be easy to slip in an
intermediate type for moving.

If it matters, I'm *only* concerned with lvalue to rvalue reference
conversion, where std::move will be used. I'm not gonna bother
with temporaries, eg:

string s = firstname + lastname;

I figure I'll just leave that as is.

Thanks again, and I'll take a look at Mojo.

--Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top