Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Function apply_visitor

boost::apply_visitor — Allows compile-time checked type-safe application of the given visitor to the content of the given variant, ensuring that all types are handled by the visitor.

Synopsis

// In header: <boost/variant/apply_visitor.hpp>


template<typename Visitor, typename Variant> 
  typename Visitor::result_type 
  apply_visitor(Visitor & visitor, Variant & operand);
template<typename Visitor, typename Variant> 
  typename Visitor::result_type 
  apply_visitor(const Visitor & visitor, Variant & operand);
template<typename BinaryVisitor, typename Variant1, typename Variant2> 
  typename BinaryVisitor::result_type 
  apply_visitor(BinaryVisitor & visitor, Variant1 & operand1, 
                Variant2 & operand2);
template<typename BinaryVisitor, typename Variant1, typename Variant2> 
  typename BinaryVisitor::result_type 
  apply_visitor(const BinaryVisitor & visitor, Variant1 & operand1, 
                Variant2 & operand2);
template<typename MultiVisitor, typename Variant1, typename Variant2, 
         typename Variant3> 
  typename MultiVisitor::result_type 
  apply_visitor(MultiVisitor & visitor, Variant1 & operand1, 
                Variant2 & operand2, Variant3 & operand3, ... other_operands);
template<typename MultiVisitor, typename Variant1, typename Variant2, 
         typename Variant3> 
  typename MultiVisitor::result_type 
  apply_visitor(const MultiVisitor & visitor, Variant1 & operand1, 
                Variant2 & operand2, Variant3 & operand3, ... other_operands);
template<typename Visitor> 
  apply_visitor_delayed_t<Visitor> apply_visitor(Visitor & visitor);

Description

The behavior of apply_visitor is dependent on the number of arguments on which it operates (i.e., other than the visitor). The function behaves as follows:

  • Overloads accepting one operand invoke the unary function call operator of the given visitor on the content of the given variant operand.
  • Overloads accepting two operands invoke the binary function call operator of the given visitor on the content of the given variant operands.
  • Overloads accepting three or more operands invoke the function call operator of the given visitor on the content of the given variant operands. Maximum amount of parameters controlled by BOOST_VARAINT_MAX_MULTIVIZITOR_PARAMS macro. Those functions are actually defined in the section called “Header <boost/variant/multivisitors.hpp>”.
  • The overload accepting only a visitor returns a generic function object that accepts either one or two arguments and invokes apply_visitor using these arguments and visitor, thus behaving as specified above. (This behavior is particularly useful, for example, when one needs to operate on each element of a sequence of variant objects using a standard library algorithm.)

Returns:

The overloads acccepting operands return the result of applying the given visitor to the content of the given operands. The overload accepting only a visitor return a function object, thus delaying application of the visitor to any operands.

Requires:

The given visitor must fulfill the StaticVisitor concept requirements with respect to each of the bounded types of the given variant.

Throws:

The overloads accepting operands throw only if the given visitor throws when applied. The overload accepting only a visitor will not throw. (Note, however, that the returned function object may throw when invoked.)

PrevUpHomeNext