Rvalue Reference Emulation
The library implements some interfaces in namespace cxxomfort
that allow the emulation of rvalue references in C++03. This, in turn, allows for such things as the emulation of move semantics and argument forwarding.
The code is based off Boost.Move, but lifted from about 20 MB of dependencies. Instead, it relies on a minimal C++03 TR1 implementation of <type_traits>
. Just like Boost's implementation, this can only offer the feature to write code using it; it can't modify existing containers such as vector, deque, to use such features - it is suggested that you use updated containers such as nononsense-boost.Container's instead. (Note: Future containers provided with cxxomfort, such as dynarray and forward_list, will use this move emulation built-in.)
The library detects if it is running in C++11 mode or not (C++11-emulation modes are not considered), and defines some types and macros to provide the features or do nothing. The following interfaces are defined:
::rvref<T>
- a wrapper type generator that defines the membertype
as either the C++03 move-emulated rvalue reference, orT&&
in C++11.std::move
- obtains a rvalue-reference to the argument and propagates it.std::forward
- forwards its argument to called code by passing a rvalue-reference.CXXO_RV_REF(type)
- a macro that outputs the rvalue-reference type for its argument, to be used in creation of eg.: move constructors.CXXO_FWD_REF(type)
- a macro that outputs the forwarding reference type for its argument, to be used in creation of eg.: emplace or (semi-)variadic argument lists.CXXO_COPY_ASSIGN_REF(type)
- a macro that outputs a type for normal, assignment operator that can be detected by this emulation.CXXO_COPYABLE_MOVABLE(type)
- a macro to be used in the body of a class to declare it as a copyable and movable type; then the adequate copy assignment and move constructor, assignment must be written.CXXO_NONCOPYABLE_MOVABLE(type)
- a macro to be used in the body of a class to declare it as a noncopyable and movable type; then the adequate move constructor, assignment must be written.std::move
algorithm - moves a range of data.std::move_backwards
algorithm - moves a range of data.
Rvalue References
The type generator ::rvref<T>
behaves as follows:
Member | In C++03 | In C++11 |
---|---|---|
type
|
::cxxomfort::rv<T>&
|
T&&
|
(For the time being see the documentation of Boost.Move)
Writing Movable Classes
(For the time being see the documentation of Boost.Move)
template <class Resource> class holds_resource { private: CXXO_COPYABLE_MOVABLE(holds_resource); Resource* ptr; public: // Construction explicit holds_resource(Resource* p = 0) : ptr(p) {} // Destruction ~holds_resource() { if (ptr) delete ptr; } // copy constructor -- works as usual holds_resource(holds_resource const& p) : ptr(p.ptr ? p.ptr->clone() : 0) {} // copy assignment -- uses assign-reference holds_resource& operator=(CXXO_COPY_ASSIGN_REF(holds_resource) p) { if (this != &p){ Resource *tmp_p = p.ptr ? p.ptr->clone() : 0; delete ptr; ptr = tmp_p; } return *this; } // move constructor -- uses rvalue-reference holds_resource(CXXO_RV_REF(holds_resource) p) : ptr(p.ptr) { p.ptr = 0; } // move assignment -- uses ralue-reference holds_resource& operator=(CXXO_RV_REF(holds_resource) p) { if (this != &p){ delete ptr; ptr = p.ptr; p.ptr = 0; } return *this; } };
Writing Emplace Argument Lists
(For the time being see the documentation of Boost.Move)
- Back to Features.