BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsltf_argumenttype

Detailed Description

Outline

Purpose

Provide a non-allocating class to test variadic function arguments.

Classes

See also
bsltf_allocargumenttype, bsltf_templatetestfacility

Description

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.

Attributes

Name Type Default Meaning
------------------ ------------------- ---------- --------------------
conversion to int int -1 value of object
copyMoveState CopyMoveState::Enum e_ORIGINAL copy/move history

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.

Usage

This section illustrates intended use of this component.

Example 1: Passing Arguments of the Correct Type and Order

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:

int delegateFunction()
{
return 0;
}
int delegateFunction(bsltf::ArgumentTypeByValue<1> arg01)
{
assert(1 == arg01);
return bsltf::CopyMoveState::isMovedInto(arg01) ? 1 : 0;
}
int delegateFunction(bsltf::ArgumentTypeByValue<1> arg01,
{
assert(1 == arg01);
assert(2 == arg02);
int ret = 0;
if (bsltf::CopyMoveState::isMovedInto(arg01)) ret += 1;
if (bsltf::CopyMoveState::isMovedInto(arg02)) ret += 2;
return ret;
}
Definition bsltf_argumenttype.h:380
static bool isMovedInto(const TYPE &v)
Definition bsltf_copymovestate.h:395

Now, we define the forwarding function we intend to test, providing both C++03 and C++11 interfaces:

#if defined(BSLS_COMPILERFEATURES_SUPPORT_VARIADIC_TEMPLATES) && \_
defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES)
template <class... Args>
inline
int forwardData(Args&&... args)
{
return delegateFunction(BSLS_COMPILERFEATURES_FORWARD(Args, args)...);
}
#else // If variadic templates or rvalue references are not supported
inline
int forwardData()
{
return delegateFunction();
}
template <class Args1>
inline
int forwardData(BSLS_COMPILERFEATURES_FORWARD_REF(Args1) args_01)
{
return delegateFunction(BSLS_COMPILERFEATURES_FORWARD(Args1, args_01));
}
template <class Args1, class Args2>
inline
int forwardData(BSLS_COMPILERFEATURES_FORWARD_REF(Args1) args_01,
{
return delegateFunction(BSLS_COMPILERFEATURES_FORWARD(Args1, args_01),
}
#endif
#define BSLS_COMPILERFEATURES_FORWARD_REF(T)
Definition bsls_compilerfeatures.h:2012
#define BSLS_COMPILERFEATURES_FORWARD(T, V)
Definition bsls_compilerfeatures.h:2018

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.

int main()
{
int ret = forwardData();
assert(0 == ret);
ret = forwardData(A01);
assert(0 == ret);
ret = forwardData(A11, bslmf::MovableRefUtil::move(A12));
assert(2 == ret);
// Note that passing arguments in a wrong order will fail to compile:
// ret = forwardData(A12, A11); // ERROR
}
Definition bsltf_argumenttype.h:238
static MovableRef< t_TYPE > move(t_TYPE &reference) BSLS_KEYWORD_NOEXCEPT
Definition bslmf_movableref.h:1060
static bool isMovedFrom(const TYPE &v)
Definition bsltf_copymovestate.h:389