BDE 4.14.0 Production release
|
Provide a non-allocating class to test variadic function arguments.
int
ArgumentType
This component provides a class, bsltf::ArgumentType<N>
, used for testing functions that take a variable number of template arguments. The integer template parameter enables specification of a number of types without requiring a separate component for each. bsltf::ArgumentType
does not allocate memory, and defines copy and move constructors and assignment operators that track whether the object was copied into, moved into, or moved from.
Note that the copy/move history is a non-saliant attribute, meaning that it is not part of the object's value and does not participate in equality comparisons.
This section illustrates intended use of this component.
Suppose we wanted to test a function, forwardData
, that takes a variable number of arguments and forwards them to another function (called delegateFunction
, in this example). Note, that the example below provides separate implementations for compilers that support C++11 and those that do not. For clarity, we define forwardData
inline and limit our C++03 expansion of the variadic template to 2 arguments.
First, we define a set of delegateFunction
overloads. The first argument, if any, is expected to have value 1
and the second argument is expected to have value 2
. The return value of delegateFunction
is 1
if the first argument is passed as an rvalue, 2
if the second argument is passed as an rvalue, 3
if they are both passed as rvalues
, and 0
otherwise. Using parameters of type ArgumentTypeByValue
allows us to verify the type, value, and value-category (rvalue vs lvalue) of each argument in a manner that is compatible with C++03:
Now, we define the forwarding function we intend to test, providing both C++03 and C++11 interfaces:
Finally, we define a test case for forwardData
passing ArgumentType
as arguments to the forwardData
function and verifying that they are perfectly forwarded to delegateFunction
.