Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bsltf_argumenttype
[Package bsltf]

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

Namespaces

namespace  bsltf

Detailed Description

Outline
Purpose:
Provide a non-allocating class to test variadic function arguments.
Classes:
bsltf::ArgumentType<N> simple wrapper around an in-place int
See also:
Component bsltf_allocargumenttype, Component 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 both copy and move constructors.
Attributes:
  Name                Type         Default
  ------------------  -----------  -------
  data                int          -1
  • data: representation of the class value
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 standard and those that do not. For clarity, we define forwardData in line, and limit our expansion of the variadic template to 2 arguments on platforms that don't support variadic templates.
First, we show the signature to the variadic function delegateFunction, that forwardData (which we wish to test) will forward to (note that the implementation has been elided for simplicity):
  void delegateFunction();
  void delegateFunction(ArgumentType<1> arg01);
  void delegateFunction(ArgumentType<1> arg01, ArgumentType<2> arg02);
Then, we define the forwarding function we intend to test:
  #if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES

  template <class... Args>
  inline
  void forwardData(Args&&... arguments) {
      delegateFunction(BSLS_COMPILERFEATURES_FORWARD(Args, arguments)...);
  }

  #elif BSLS_COMPILERFEATURES_SIMULATE_VARIADIC_TEMPLATES

  inline
  void forwardData()
  {
      delegateFunction();
  }

  template <class Args_01>
  inline
  void forwardData(BSLS_COMPILERFEATURES_FORWARD_REF(Args_01)  arguments_01)
  {
      delegateFunction(BSLS_COMPILERFEATURES_FORWARD(Args_01, arguments_01));
  }

  template <class Args_01, class Args_02>
  inline
  void forwardData(BSLS_COMPILERFEATURES_FORWARD_REF(Args_01)  arguments_01,
                   BSLS_COMPILERFEATURES_FORWARD_REF(Args_02)  arguments_02)
  {
      delegateFunction(BSLS_COMPILERFEATURES_FORWARD(Args_01, arguments_01),
                       BSLS_COMPILERFEATURES_FORWARD(Args_02, arguments_02));
  }

  #else

  // The code below is a workaround for the absence of perfect forwarding in
  // some compilers.
  template <class... Args>
  inline
  void forwardData(BSLS_COMPILERFEATURES_FORWARD_REF(Args)... arguments)
  {
      delegateFunction(BSLS_COMPILERFEATURES_FORWARD(Args, arguments)...);
  }

  #endif
Finally, we define a test case for forwardData passing ArgumentType as variadic arguments to the forwardData function and verifying that compilation succeeds:
  void usageExample()
  {
      forwardData();

      ArgumentType<1> A01(1);
      forwardData(A01);

      ArgumentType<1> A11(13);
      ArgumentType<2> A12(28);
      forwardData(A11, A12);

      // Note that passing arguments in a wrong order will fail to compile:
      // ArgumentType<1> A21(3);
      // ArgumentType<2> A22(82);
      // forwardData(A22, A21);
  }