Provide a meta-function for adding "reference-ness" to a type.
More...
Detailed Description
- Outline
-
-
- Purpose:
- Provide a meta-function for adding "reference-ness" to a type.
-
- Classes:
-
-
- Description:
- This component defines a simple template
struct
, bslmf::AddReference
, that is used to define a reference type from the type supplied as its single template type parameter. Types that are void
or already reference types are unmodified.
-
- Usage:
- This section illustrates intended use of this component.
-
- Example 1: A Simple Wrapper Class:
- First, let us write a simple class that can wrap any other type:
template <class t_TYPE>
class Wrapper {
private:
t_TYPE d_data;
public:
typedef typename bslmf::AddReference<t_TYPE>::Type WrappedType;
Wrapper(t_TYPE value) : d_data(value) {}
Then, we would like to expose access to the wrapped element through a method that returns a reference to the data member d_data
. However, there would be a problem if the user supplied a parameterized type t_TYPE
that is a reference type, as references-to-references were not permitted by the language (prior the C++11 standard). We can resolve such problems using the meta-function bslmf::AddReference
. Next, we supply an accessor function, value
, that similarly wraps the parameterized type t_TYPE
with the bslmf::AddReference
meta-function. In this case we must remember to const-quality t_TYPE
before passing it on to the meta-function. Now, we write a test function, runTest
, to verify our simple wrapper type. We start by wrapping a simple int
value: void runTests()
{
int i = 42;
Wrapper<int> ti(i); const Wrapper<int>& TI = ti;
assert(42 == i);
assert(42 == TI.value());
ti.value() = 13;
assert(42 == i);
assert(13 == TI.value());
Finally, we test Wrapper
with a reference type: Wrapper<int&> tr(i); const Wrapper<int&>& TR = tr;
assert(42 == i);
assert(42 == TR.value());
tr.value() = 13;
assert(13 == i);
assert(13 == TR.value());
i = 42;
assert(42 == i);
assert(42 == TR.value());
}
-
- Example 2: Expected Results:
- For this example, the associated comments below indicate the expected type of
bslmf::AddReference::Type
for a broad range of parameterized types:
Define Documentation