Aim: This class encapsulates its parameter class so that to indicate to the user that the object/pointer will be only aliased. Therefore the user is reminded that the argument parameter is given to the function without any additional cost and may be modified, while he is aware that the lifetime of the argument parameter must be at least as long as the object itself. Note that an instance of Alias<T> is itself a light object (it holds only an enum and a pointer).
More...
template<typename T>
class DGtal::Alias< T >
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/pointer will be only aliased. Therefore the user is reminded that the argument parameter is given to the function without any additional cost and may be modified, while he is aware that the lifetime of the argument parameter must be at least as long as the object itself. Note that an instance of Alias<T> is itself a light object (it holds only an enum and a pointer).
Description of template class 'Alias'
(For a complete description, see Parameter passing, cloning and referencing).
It is used in methods or functions to encapsulate the parameter types. The following conversion from input parameter to data member or variable are possible:
Argument type | T& | T* | CountedPtr<T> | CountedPtrOrPtr<T> |
To: T& | Shared. O(1) | Shared. O(1) | | |
To: T* | Shared. O(1) | Shared. O(1) | | |
To: CountedPtrOrPtr<T> | Shared. O(1) | Shared. O(1) | Shared. O(1), secure | Shared. O(1), secure |
Argument conversion to member is automatic except when converting to a pointer T*
: the address operator (operator&
) must be used in this case.
For the last row (case where the programmer choose a CountedPtrOrPtr to hold the const alias), the user can thus enforce a secure aliasing by handling a variant of CountedPtr as argument. In this case, even if the aliased object is destroyed in the caller context, it still exists in the callee context.
- Note
- The usage of Alias<T> instead of
T
&
or T
*
in parameters is recommended when the lifetime of the parameter must exceed the lifetime of the called method/function/constructor (often the case in constructor or init methods).
-
The usage of
T
&
or T
*
instead of Alias<T> is recommended when the lifetime of the parameter is not required to exceed the lifetime of the called method/function/constructor (often the case in standard methods, where the parameter is only used at some point, but not referenced after in some data member).
- Template Parameters
-
- See also
- ConstAlias
-
Clone
It can be used as follows. Consider this simple example where class A is a big object.
const int N = 10000;
struct A { ...
int table[N];
};
struct B1 {
B1( A & a )
: myA( a ) {}
...
A & myA;
};
Sometimes it is very important that the developper that uses the library is conscious that an object, say b, may require that an instance a given as parameter should have a lifetime longer than b itself (case for an instance of B1 above). Classes Clone, Alias, ConstAlias exist for these reasons. The class above may be rewritten as follows.
struct B1_v2_1 {
B1_v2_1( Alias<A> a )
: myA( a ) {}
...
A & myA;
};
struct B1_v2_2 {
B1_v2_2( Alias<A> a )
: myA( &a ) {}
...
A* myA;
};
struct B1_v2_3 {
B1_v2_3( Alias<A> a )
: myA( a ) {}
...
CountedPtrOrPtr<A> myA;
};
...
A a1;
CountedPtr<A> cptr_a1( new A( a1 ) );
B1 ( a1 );
B1_v2_1 ( a1 );
B1_v2_2 ( a1 );
B1_v2_3 ( a1 );
B1_v2_3 ( cptr_a1 );
- Note
- The user should not use Alias<T> instead of T* for data members. It works in most cases, but there are some subtle differences between the two behaviors.
-
Alias have a default copy constructor, so as to let the user forward an Alias<T> parameter.
Definition at line 182 of file Alias.h.