1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
Before using the reference wrappers discussed in this section the
tthi(functional) header file must have been included.
Situations exist where the compiler is unable to infer that a reference rather
than a value is passed to a template function. In the following example the
template function tt(outer) receives tt(int x) as its argument and the
compiler will dutifully infer that tt(Type) is tt(int):
verbinsert(INT)(functiontemplates/examples/refwrap.cc)
Compilation will of course fail and the compiler nicely reports the
inferred type, e.g.:
verb(
In function 'void outer(Type) [with Type = int]': ...
)
Unfortunately the same error is generated when using tt(call) in the next
example. The function tt(call) is a template expecting a function that takes
an argument which is then itself modified, and a value to pass on to that
function. Such a function is, e.g., tt(sqrtArg) expecting a reference to a
tt(double), which is modified by calling tt(std::sqrt).
verbinsert(DOUBLE)(functiontemplates/examples/refwrap.cc)
Assuming tt(double value = 3) then tt(call(sqrtArg, value)) will not modify
tt(value) as the compiler infers tt(Arg) to be tt(double) and hence passes
tt(value) by value.
To have tt(value) itself changed the compiler must be informed that tt(value)
must be passed by reference. Note that it might not be acceptable to define
tt(call)'s template argument as tt(Arg &) as em(not) changing the actual
argument might be appropriate in some situations.
The C++0x standard offers the ti(CHAR(r)ef(arg)) and ti(cref(arg))
hi(reference wrapper)em(reference wrappers) that take an argument and
return it as a reference-typed argument. To actually change tt(value) it can
be passed to tt(call) using tt(ref(value)) as shown in the following tt(main)
function:
verbinsert(MAIN)(functiontemplates/examples/refwrap.cc)
|