NABLA
Nabla Ain't Basic Linear Algebra
|
Temporary object template class. More...
Public Member Functions | |
temporary () | |
Default constructor. | |
temporary (temporary &a) | |
Move constructor. | |
template<typename other > | |
temporary (const other &a) | |
Template copy constructor. | |
temporary & | operator= (temporary &a) |
Move assignment operator. | |
template<typename other > | |
temporary & | operator= (const other &a) |
Template assignment operator. |
Temporary object template class.
This is a factorization and generalization of temporary object. It can be used with a class or struct allowing this tandem to process temporary objects without significant performance overhead.
In absence of a mechanism that efficiently handles temporary objects this template class is very useful if one follows a few simple rules:
temporary<type>
as a return value;Let's look closer at each item. There are 99.(9)% of chance that your type can implement efficient swap() function with constant time complexity, e.g. for a sequence it can be just swapping of pointers (no copy operations!). For example all standard containers in the STL provide such function so you can easily use this template class to eliminate copying while returning a value.
Actually usage of this template is quite trivial, look yourself:
There is only one copy operation – assignment operator. When return value ret
is returned from foo()
its contents are passed to implicitly introduced temporary object with help of swap() function. This way the overhead of returning a value from function is eliminated.
At last if you want to eliminate ridiculous copy overhead when 'copying' from temporary objects you have to implement copy constructor and assignment operator with move semantics. Sounds sound but there is nothing difficult. Just add to the definition of your type something like
And that's all! Now your type handles temporary objects in a very efficient manner. Statements like
literally turns into
Thus contents of ret
are effectively passed to result
without copy operations.
Move constructor.
Actual effect is
Definition at line 2239 of file matrix.h.
References nabla::swap().
|
inline |
Move assignment operator.
Actual effect is
Definition at line 2252 of file matrix.h.
References nabla::swap().
|
inline |
Template assignment operator.
Actual effect is type::operator=(a)
. If type
has no accessible assignment operator which takes object of type other
code doesn't compile.
Definition at line 2263 of file matrix.h.
References temporary< type >::operator=().