Skip navigation.

Tip: Class any, a C++ variant value type, adapted for VC6

Class any is a C++ variant type. It's a nifty class written by Kevlin Henney. In 2000, it appeared in the article Valued Conversions of the C++ report issue of July–August [1]. Somewhat earlier, class any also played a role in the presentations Substitutability. Principles, Idioms and Techniques for C++ [2] and Idioms. Breaking the Language Barrier [3].

When I tried to compile the code from [1] with Visual C++ 6, it struggled with the converting constructor, the assignment operators and with the member to_ptr() that is used by custom keyword cast any_cast.

I made a few simple changes –once again– to please VC6 SP6 to let it compile. To make the converting constructor usable, I made the copy-assignment constructor different from it via an extra defaulted parameter [4]. To differentiate between converting assignment operator and plain assignment operator, the latter was changed to use the copy-and-swap idiom [5,6]. This change was gleaned from an old version of Boost.Any [7,8]. Finally, I changed template member to_ptr() to to_ptr(value_type *) to let any_cast transfer value_type to it.

Public interface of class any and custom keyword casts.

The following example is adapted from Boost.Any:

The following predicates follow on from the previous definitions and demonstrate the use of queries on any objects:

The program using it:

The output of above program:

Notes and References.
[1] Kevlin Henney. Valued Conversions (PDF). C++ report, July–August 2000.
[2] Kevlin Henney. Substitutability. Principles, Idioms and Techniques for C++ (PDF). Presented at JaCC, Oxford, 16th September 1999.
[3] Kevlin Henney. Idioms. Breaking the Language Barrier (PDF). Presented at the ACCU's C and C++ European Developers Forum, the Oxford Union, Oxford, UK, 12th September 1998.
[4] Martin Moene. How to fix VC6 templated converting constructor.
[5] Martin Moene. How to fix VC6 templated converting assignment operator.
[6] Boost.Any. Safe, generic container for single values of different value types.
Version 1.34 is the last version of the Boost libraries with a VC6-compatible version of any [5].
[7] StackOverflow. What is the copy-and-swap idiom?
[8] StackOverflow. What's the latest version of Boost compatible with VC++6?
[9] Boost. Portability Hints: Microsoft Visual C++ 6.0 SP4.

Page created 19 June 2012