Warning: This is an old version. The latest stable version is Version 7.1.1.

class stub::function< R(Args…)>

In header: #include <stub/function.hpp>

Brief description

The function object act like a “sink” for function calls i.e. we can define a function object to accept any type of function call and it will simply store the arguments for later inspection.

Template parameters

template <typename R, typename... Args>
class function< R(Args...)>

Member types (public)

struct expectation

Member functions (public)

R operator() (Args… args) const
return_handler< R > & set_return (Returns &&… return_value)
uint32_t calls () const
bool no_calls () const
const arguments< Args… > & call_arguments (uint32_t index) const
expectation expect_calls () const
void clear ()
void clear_calls ()
void print (std::ostream & out) const


The typical use-case for the function object is when testing that some code invokes a specific set of functions with a specific set of arguments.


function<void(uint32_t)> some_function;

The above function takes an uint32_t and returns nothing, lets invoke it:


Now we may check how the function was called:

bool called_once = some_function.expect_calls().with(3U);
assert(called_once == false);

bool called_with = some_function.expect_calls().with(4U);
assert(called_with == true);

We can also define a function which returns a value:

function<bool(uint32_t)> another_function;

Here we have to specify what return value we expect:


bool a = another_function(23);
bool b = another_function(13);

assert(a == true);
assert(b == true);

For more information on the options for return values see the src/stub/return_handler.hpp

Member Function Descriptions

R operator() (Args… args) const

The call operator to “simulate” performing a function call.

Parameter args:
The arguments that should be stored
The return value generated by the return_handler

template <class… Returns>
return_handler< R > & set_return (Returns &&… return_value)

Initializes the return_handler with the return values to use. Calling this function will also reset the return_handler state. So any previously specified returns values will be removed etc.

Parameter return_value:
The list of return values to use
Reference to the return handler, this allows the caller to perform additional customization to the return handler such as turn on or off repeat.

uint32_t calls () const
The number of times the call operator has been invoked

bool no_calls () const
True if no calls have been made otherwise false

const arguments< Args… > & call_arguments (uint32_t index) const
The arguments passed to the n’th call

expectation expect_calls () const

Used when we want to check whether the function object is in a certain state. See examples usage in the expectation struct member functions.

An expectation object

void clear ()
Removes all calls from the function object and reset the return handler.

void clear_calls ()
Clear the calls.

void print (std::ostream & out) const

Prints the status of the function object to the std::ostream.

Example (using the output operator):

function<void(uint32_t)> my_func;


// Print the current status of the function object,
std::cout << my_func << std::endl;
Parameter out:
The ostream where the function status should be