Boost logo

Boost :

From: Corwin Joy (cjoy_at_[hidden])
Date: 2001-07-19 02:10:17


I have uploaded to the boost website a set of files for registering and
performing conversion casts with an 'any' object into the folder any_ccast.
This can be done without making modifications to the current 'any'
structure. The syntax for this cast looks like this:

any_ccast ('any' converting cast)

template<typename ValueType>
  ValueType any_ccast(const any & operand);

Custom keyword cast for extracting a value of a given type from an any with
a possible conversion to get the requested type. If the conversion from the
type held by any to the requested value type is allowed then this returns a
copy of the extracted result if successful, otherwise a bad_cast exception
is thrown. any_ccast is normally used when you don't know the exact type
held by an 'any' variable and you want to request a (possibly) converting
cast to a given format.

Examples:

any a(double(3.5));

// These will invoke boost::numeric_cast
cout << any_ccast<int>(a) << endl; // cast out 'any' contents to an int
cout << any_ccast<float>(a) << endl; // cast out 'any' contents to a float

// This will use boost::lexical_cast
cout << any_ccast<string>(a) << endl; // cast out 'any' contents to string

similarly, one can write:

any a(string("12345"));
cout << any_ccast<int>(a) << endl; // lexical cast to an int

etc.

For more examples see the documentation and tests in the uploaded folder.

Users can also add their own classes and user defined casts as shown in the
cast_registry constructor in any_ccast.cpp. Essentially the syntax is just:

g_cast_registry.add_cast_fn(id_for_target_type, id_for_source_type,
  pointer_to_cast_function);

see any_ccast.cpp for details.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk