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

Click here to view the latest version of this page.
PrevUpHomeNext

Reference

Concepts
Header <boost/variant.hpp>
Header <boost/variant/variant_fwd.hpp>
Header <boost/variant/variant.hpp>
Header <boost/variant/recursive_variant.hpp>
Header <boost/variant/recursive_wrapper.hpp>
Header <boost/variant/apply_visitor.hpp>
Header <boost/variant/get.hpp>
Header <boost/variant/bad_visit.hpp>
Header <boost/variant/static_visitor.hpp>
Header <boost/variant/visitor_ptr.hpp>

Concepts

BoundedType

The requirements on a bounded type are as follows:

  • CopyConstructible [20.1.3].
  • Destructor upholds the no-throw exception-safety guarantee.
  • Complete at the point of variant template instantiation. (See boost::recursive_wrapper<T> for a type wrapper that accepts incomplete types to enable recursive variant types.)

Every type specified as a template argument to variant must at minimum fulfill the above requirements. In addition, certain features of variant are available only if its bounded types meet the requirements of these following additional concepts:

  • Assignable: variant is itself Assignable if and only if every one of its bounded types meets the requirements of the concept. (Note that top-level const-qualified types and reference types do not meet these requirements.)
  • DefaultConstructible [20.1.4]: variant is itself DefaultConstructible if and only if its first bounded type (i.e., T1) meets the requirements of the concept.
  • EqualityComparable: variant is itself EqualityComparable if and only if every one of its bounded types meets the requirements of the concept.
  • LessThanComparable: variant is itself LessThanComparable if and only if every one of its bounded types meets the requirements of the concept.
  • OutputStreamable: variant is itself OutputStreamable if and only if every one of its bounded types meets the requirements of the concept.

StaticVisitor

The requirements on a static visitor of a type T are as follows:

  • Must allow invocation as a function by overloading operator(), unambiguously accepting any value of type T.
  • Must expose inner type result_type. (See boost::visitor_ptr for a solution to using functions as visitors.)
  • If result_type is not void, then each operation of the function object must return a value implicitly convertible to result_type.
Examples

The following class satisfies the requirements of a static visitor of several types (i.e., explicitly: int and std::string; or, e.g., implicitly: short and const char *; etc.):

class my_visitor
    : public boost::static_visitor<int>
{
public:

    int operator()(int i)
    {
        return i * 2;
    }

    int operator()(const std::string& s)
    {
        return s.length();
    }

};

Another example is the following class, whose function-call operator is a member template, allowing it to operate on values of many types. Thus, the following class is a visitor of any type that supports streaming output (e.g., int, double, std::string, etc.):

class printer
    : public boost::static_visitor<>
{
    template <typename T>
    void operator()(const T& t)
    {
        std::cout << t << std::endl;
    }
};

OutputStreamable

The requirements on an output streamable type T are as follows:

  • For any object t of type T, std::cout << t must be a valid expression.

This header exists simply as a convenience to the user, including all of the headers in the boost/variant directory.

Provides forward declarations of the boost::variant, boost::make_variant_over, boost::make_recursive_variant, and boost::make_recursive_variant_over class templates and the boost::recursive_variant_ tag type. Also defines several preprocessor symbols, as described below.


BOOST_VARIANT_LIMIT_TYPES
BOOST_VARIANT_ENUM_PARAMS(param)
BOOST_VARIANT_ENUM_SHIFTED_PARAMS(param)
BOOST_VARIANT_NO_REFERENCE_SUPPORT
BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT
BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT
namespace boost {
  template<typename T1, typename T2 = unspecified, ..., 
           typename TN = unspecified> 
    class variant;

  template<typename Sequence> class make_variant_over;
  template<typename T1, typename T2, ..., typename TN> 
    void swap(variant<T1, T2, ..., TN> &, variant<T1, T2, ..., TN> &);
  template<typename ElemType, typename Traits, typename T1, typename T2, ..., 
           typename TN> 
    std::basic_ostream<ElemType,Traits> & 
    operator<<(std::basic_ostream<ElemType,Traits> &, 
               const variant<T1, T2, ..., TN> &);
}
namespace boost {
  typedef unspecified recursive_variant_;

  template<typename T1, typename T2 = unspecified, ..., 
           typename TN = unspecified> 
    class make_recursive_variant;
  template<typename Sequence> class make_recursive_variant_over;
}
namespace boost {
  template<typename T> class recursive_wrapper;
  template<typename T> class is_recursive_wrapper;
  template<typename T> class unwrap_recursive_wrapper;
}
namespace boost {
  template<typename Visitor> class apply_visitor_delayed_t;
  template<typename Visitor, typename Variant> 
    typename Visitor::result_type apply_visitor(Visitor &, Variant &);
  template<typename Visitor, typename Variant> 
    typename Visitor::result_type apply_visitor(const Visitor &, Variant &);
  template<typename BinaryVisitor, typename Variant1, typename Variant2> 
    typename BinaryVisitor::result_type 
    apply_visitor(BinaryVisitor &, Variant1 &, Variant2 &);
  template<typename BinaryVisitor, typename Variant1, typename Variant2> 
    typename BinaryVisitor::result_type 
    apply_visitor(const BinaryVisitor &, Variant1 &, Variant2 &);
  template<typename Visitor> 
    apply_visitor_delayed_t<Visitor> apply_visitor(Visitor &);
}
namespace boost {
  class bad_get;
  template<typename U, typename T1, typename T2, ..., typename TN> 
    U * get(variant<T1, T2, ..., TN> *);
  template<typename U, typename T1, typename T2, ..., typename TN> 
    const U * get(const variant<T1, T2, ..., TN> *);
  template<typename U, typename T1, typename T2, ..., typename TN> 
    U & get(variant<T1, T2, ..., TN> &);
  template<typename U, typename T1, typename T2, ..., typename TN> 
    const U & get(const variant<T1, T2, ..., TN> &);
}
namespace boost {
  class bad_visit;
}
namespace boost {
  template<typename ResultType> class static_visitor;
}
namespace boost {
  template<typename T, typename R> class visitor_ptr_t;
  template<typename R, typename T> visitor_ptr_t<T,R> visitor_ptr(R (*)(T));
}

PrevUpHomeNext