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

PrevUpHomeNext

Using Any

Construction
Conversions
References
Syntax Limitations

(For the source of the examples in this section see construction.cpp)

The library provides the constructible concept to allow an any to capture constructors. The single template argument should be a function signature. The return type must be a placeholder specifying the type to be constructed. The arguments are the arguments of the constructor.

typedef mpl::vector<
    copy_constructible<_a>,
    copy_constructible<_b>,
    copy_constructible<_c>,
    constructible<_a(const _b&, const _c&)>
> construct;

typedef mpl::map<
    mpl::pair<_a, std::vector<double> >,
    mpl::pair<_b, std::size_t>,
    mpl::pair<_c, double>
> types;

any<construct, _b> size(std::size_t(10), make_binding<types>());
any<construct, _c> val(2.5, make_binding<types>());
any<construct, _a> v(size, val);
// v holds std::vector<double>(10, 2.5);

Now, suppose that we want a default constructor? We can't have the default constructor of any call the default constructor of the contained type, because it would have no way of knowing what the contained type is. So, we'll need to pass the placeholder binding information explicitly.

typedef mpl::vector<
    copy_constructible<>,
    constructible<_self()>
> construct;

any<construct> x(std::string("Test"));
any<construct> y(binding_of(x)); // y == ""

This method is not restricted to the default constructor. If the constructor takes arguments, they can be passed after the bindings.

typedef mpl::vector<
    copy_constructible<>,
    constructible<_self(std::size_t, char)>
> construct;

any<construct> x(std::string("Test"));
any<construct> y(binding_of(x), 5, 'A');

(For the source of the examples in this section see convert.cpp)

An any can be converted to another any as long as the conversion is an "upcast."

typedef any<
    mpl::vector<
        copy_constructible<>,
        typeid_<>,
        ostreamable<>
    >
> any_printable;
typedef any<
    mpl::vector<
        copy_constructible<>,
        typeid_<>
    >
> common_any;
any_printable x(10);
common_any y(x);

This conversion is okay because the requirements of common_any are a subset of the requirements of any_printable. Conversion in the other direction is illegal.

common_any x(10);
any_printable y(x); // error

(For the source of the examples in this section see references.cpp)

To capture by reference, we simply add a reference to the placeholder.

int i;
any<typeid_<>, _self&> x(i);
any_cast<int&>(x) = 5; // now i is 5

[Note] Note

_self is the default placeholder, so it is easiest to use _self&. We could use another placeholder instead. any<typeid_<_a>, _a&> has exactly the same behavior.

References cannot be rebound. Just like a built-in C++ reference, once you've initialized it you can't change it to point to something else.

int i, j;
any<typeid_<>, _self&> x(i), y(j);
x = y; // error

[Note] Note

As with any other operation, x = y for references acts on i and j. Assignment like this is legal if assignable<> is in the Concept, but x would still hold a reference to i.

A reference can be bound to another any.

typedef mpl::vector<
    copy_constructible<>,
    incrementable<>
> requirements;

any<requirements> x(10);
any<requirements, _self&> y(x);
++y; // x is now 11

If a reference is used after the underlying object goes out of scope or is reset, the behavior is undefined.

typedef mpl::vector<
    copy_constructible<>,
    incrementable<>,
    relaxed
> requirements;
any<requirements> x(10);
any<requirements, _self&> y(x);
x = 1.0;
++y; // undefined behavior.

This only applies when a reference is constructed from a value. If a reference is constructed from another reference, the new reference does not depend on the old one.

any<requirements> x(10);
boost::shared_ptr<any<requirements, _self&> > p(
    new any<requirements, _self&>(x));
any<requirements, _self&> y(*p); // equivalent to y(x);
p.reset();
++y; // okay

Both const and non-const references are supported.

int i = 0;
any<incrementable<>, _self&> x(i);
any<incrementable<>, const _self&> y(x);

A reference to non-const can be converted to a reference to const, but not the other way around. Naturally, we can't apply mutating operations to a const reference.

any<incrementable<>, _self&> z(y); // error
++y; // error

In most cases using an any has the same syntax as using the underlying object. However, there are a few cases where this is not possible to implement. An any reference is proxy and cannot be used in contexts where a real reference is required. In particular, forward_iterator does not create a conforming ForwardIterator (unless the value_type is fixed.) Another difference is that all operations which do not take at least one any argument have to be passed the type information explicitly. Static member functions and constructors can fall in this category. All this means that generic algorithms might not work when applied to any arguments.


PrevUpHomeNext