Any Library Proposal (Revision 1)

This paper proposes a type-safe container for single values of value types
that is suitable for a Technical Specification (TS), the C++ Standard itself
(C++1Y), or a new International Standard (IS).

The proposal is based on the Boost Any Library (see
www.boost.org/libs/any). The Boost version of the library
has been in wide use for over a decade. The
proposal is a pure
addition to the standard library, requires no modifications of existing headers, and,
modulo the usual namespace caveats, will have no effect on existing code.

There are times when a generic (in the sense of
general as opposed to template-based
programming) type is needed: variables that are truly variable,
accommodating values of many other more specific types rather than C++'s normal
strict and static types. We can distinguish three basic kinds of generic type:

Converting types that can hold one of a number of possible value types,
e.g. int and
string, and freely convert between them, for instance interpreting
5 as "5"
or vice-versa. Such types are common in scripting and other interpreted
languages. boost::lexical_cast supports
such conversion functionality.

Discriminated types that contain values of different types but do not
attempt conversion between them, i.e. 5 is
held strictly as an int and is not
implicitly convertible either to "5" or to
5.0. Their indifference to interpretation
but awareness of type effectively makes them safe, generic containers of
single values, with no scope for surprises from ambiguous conversions.

Indiscriminate types that can refer to anything but are oblivious to the
actual underlying type, entrusting all forms of access and interpretation to
the programmer. This niche is dominated by void *,
which offers plenty of scope for surprising, undefined behavior.

The proposed any class (based on the class of the same name
described in
"Valued Conversions" by Kevlin Henney, C++ Report
12(7), July/August 2000) is a variant value type based on the second category.
It supports copying of any value type and safe checked extraction of that value
strictly against its type.

A similar design, offering more appropriate operators, could be used for a
generalized function adaptor, a generalized iterator adaptor, and other
object types that need uniform runtime treatment but support only compile-time
template parameter conformance. Such components are not proposed here.

Changed any assignment from ValueType to pass by
value instead of const reference (Sean Parent).

Changed any_cast with pointer argument to require
that the template parameter itself be a pointer, to match dynamic_cast.
(Thanks to Sean for catching this.) The Boost interface added *
to the return type solely to cope with historical compiler issues, and this
workaround had crept into the proposal.

Made several small editorial changes to improve standardese.

Sean Parent of Adobe Systems kindly provided his C++11 version of
the any interface to aid in preparing this proposal. Sean has done
extensive experiments with real C++11 compilers on issues such as pass-by-value
versus pass-by-reference. Changes annotated above are strongly influenced by his
suggestions.

The following base class demonstrates one approach to runtime polymorphism
based callbacks that also require arbitrary argument types. The absence of
virtual member templates requires that different solutions have different
trade-offs in terms of efficiency, safety, and generality. Using a checked
variant type offers one approach:

This clause describes components that C++ programs may use to perform
operations on objects of a discriminated type.

[Note: The discriminated type may contain values of different types
but does not attempt conversion between them, i.e.
5 is held strictly as an int and is
not implicitly convertible either to "5" or
to 5.0. This indifference to interpretation
but awareness of type effectively allows safe, generic containers of single
values, with no scope for surprises from ambiguous conversions. -- end note.]

A ValueType type shall meet the requirements for CopyConstructible
[copyconstructible]. The strong exception-safety guarantee is required for all
forms of assignment.

[Note: Values are strongly informational objects for which identity is not significant,
i.e. the focus is principally on their state content and any behavior organized
around that. Another distinguishing feature of values is their granularity:
normally fine-grained objects representing simple concepts in the system such as
quantities.

As the emphasis of a value lies in its state not its identity, values can be
copied and typically assigned one to another, requiring the explicit or implicit
definition of a public copy constructor and public assignment operator. Values
typically live within other scopes, i.e. within objects or blocks, rather than
on the heap. Values are therefore normally passed around and manipulated
directly as variables or through references, but not as pointers that emphasize
identity and indirection. --end note]