Public Types | Public Member Functions | Private Attributes | List of all members
ecl::ReferenceWrapper< T > Class Template Reference

Provides a wrapper which allows the original object to be passed like a reference. More...

#include <references.hpp>

Public Types

typedef T type
 

Public Member Functions

 operator T& () const
 Convenience operator for converting to the underlying reference directly. More...
 
T * pointer () const
 Accessor to a pointer to the original instance. More...
 
T & reference () const
 Accessor to the reference of the original instance. More...
 
 ReferenceWrapper (T &t)
 Constructs the wrapper around the supplied object instance. More...
 
virtual ~ReferenceWrapper ()
 

Private Attributes

T * obj_ptr
 

Detailed Description

template<typename T>
class ecl::ReferenceWrapper< T >

Provides a wrapper which allows the original object to be passed like a reference.

This class wraps the supplied object, storing it as a pointer and allowing it to be passed like a regular object. By itself, this is not terribly useful, but in combination with the ref/cref functions and also the is_reference_wrapper trait, it allows things like functions and function objects to be easily manipulated.

Note: reference wrappers might seem a bit redundant - why do we need them? The following case illustrates a desired usage scenario.

class A {
public:
template <typename T>
void f(T i) { cout << "Not a reference input" << endl; }
template <typename T>
void f(T &i) { cout << "A reference input" << endl; }
}

Unfortunately, when you come to call this function, you'll get an ambiguity error.

In these situations you can use a simple function call (not ref) and pass in this reference wrapper:

class A {
public:
template <typename T>
void f(const T &t) {
if ( is_reference_wrapper<T> ) {
// do something for references
} else {
// do something for non-references
}
}
}
Template Parameters
T: the type of object this class is wrapping.

Definition at line 91 of file references.hpp.

Member Typedef Documentation

◆ type

template<typename T >
typedef T ecl::ReferenceWrapper< T >::type

Convenient handle to the reference wrapper's base type.

Definition at line 94 of file references.hpp.

Constructor & Destructor Documentation

◆ ReferenceWrapper()

template<typename T >
ecl::ReferenceWrapper< T >::ReferenceWrapper ( T &  t)
inline

Constructs the wrapper around the supplied object instance.

Constructs the wrapper around the supplied object instance.

Parameters
t: the object instance.

Definition at line 103 of file references.hpp.

◆ ~ReferenceWrapper()

template<typename T >
virtual ecl::ReferenceWrapper< T >::~ReferenceWrapper ( )
inlinevirtual

Definition at line 105 of file references.hpp.

Member Function Documentation

◆ operator T&()

template<typename T >
ecl::ReferenceWrapper< T >::operator T& ( ) const
inline

Convenience operator for converting to the underlying reference directly.

When used where the underlying reference is expected, this automatically handles the conversion.

Returns
T& : the returned reference to the wrapped object.

Definition at line 115 of file references.hpp.

◆ pointer()

template<typename T >
T* ecl::ReferenceWrapper< T >::pointer ( ) const
inline

Accessor to a pointer to the original instance.

Returns a pointer to the wrapped object.

Returns
T& : the returned pointer to the wrapped object.

Definition at line 133 of file references.hpp.

◆ reference()

template<typename T >
T& ecl::ReferenceWrapper< T >::reference ( ) const
inline

Accessor to the reference of the original instance.

Returns a reference to the wrapped object.

Returns
T& : the returned reference to the wrapped object.

Definition at line 124 of file references.hpp.

Member Data Documentation

◆ obj_ptr

template<typename T >
T* ecl::ReferenceWrapper< T >::obj_ptr
private

Definition at line 136 of file references.hpp.


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


ecl_utilities
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:32