NABLA  Nabla Ain't Basic Linear Algebra
Public Member Functions | List of all members
temporary< type > Class Template Reference

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.
temporaryoperator= (temporary &a)
 Move assignment operator.
template<typename other >
temporaryoperator= (const other &a)
 Template assignment operator.

Detailed Description

template<typename type>
class nabla::temporary< type >

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.

Usage of temporary< >

In absence of a mechanism that efficiently handles temporary objects this template class is very useful if one follows a few simple rules:

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:

typedef std::place_your_favourite_here container;
temporary<container> foo(const container &c)
{
temporary<container> ret; //could be 'ret(c)' if you desire
//...
return ret;
}
//...
result = foo(bar); //somewhere in code

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

type(temporary<type> &t)
{
//default construction of 'type'
this->swap(t); //assuming 'swap()' is "the one" function mentioned above
}
//...
type &operator=(temporary<type> &t)
{
this->swap(t);
return *this;
}

And that's all! Now your type handles temporary objects in a very efficient manner. Statements like

temporary<my_type> process(const my_type &item)
{
temporary<my_type> ret;
//processing item
return ret;
}
//...
my_type result = process(item); //assuming my_type is aware of temporary<>

literally turns into

my_type ret; //in 'process()'
//processing item
my_type ret_val; //implicitly introduced temporary object
swap(ret_val, ret); //returning from 'process()'
my_type result;
swap(result, ret_val); //actual effect of assignment operator

Thus contents of ret are effectively passed to result without copy operations.

A bit of dreaming...
Well, when C++1x comes out... yeah... and if it has the feature of determining temporary objects like
type(type &&temp); //move constructor
you'd be able to throw those crutches away and just change
temporary<type> &
in your type definition to
type &&
Ahh... dreaming, dreaming...

Definition at line 2232 of file matrix.h.

Constructor & Destructor Documentation

temporary ( temporary< type > &  a)
inline

Move constructor.

Actual effect is

swap(*this, a)

Definition at line 2239 of file matrix.h.

References nabla::swap().

temporary ( const other &  a)
inline

Template copy constructor.

Actual effect is type(a) construction. If type has no accessible constructor which takes object of type other code doesn't compile.

Definition at line 2249 of file matrix.h.

Member Function Documentation

temporary& operator= ( temporary< type > &  a)
inline

Move assignment operator.

Actual effect is

swap(*this, a)

Definition at line 2252 of file matrix.h.

References nabla::swap().

temporary& operator= ( const other &  a)
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=().


The documentation for this class was generated from the following file: