From: Daryle Walker (darylew_at_[hidden])
Date: 2000-09-02 00:54:11
Neat class! I wonder if something like this is how languages (usually minor
ones) that have a "any"-like type do it?
I have one major complaint: change the name of the "type_info" method!
- I like the names of functions and methods to convey some sort of action.
"type_info" sounds wimpy and/or too much like a noun.
- The "type_info" name is already used (as a noun; a class name) in the std
namespace, so it looks confusing.
- The previous point is made worse by the fact that the "type_info" class is
the return type of the "type_info" method!
A much better name would be something like "get_type." (You could also go
all the way and make it a conversion operator [to const std::type_info &],
but I don't think "any" should be convertible to that class.)
- Why have multiple, consecutive "public" sections? It looks like you are
doing it to section your methods, but the comments alone will do it.
- You probably don't need all those access methods. You can get rid of
"copy_to" and "to_ptr" and make people live with "any_cast" and exceptions.
The "any_cast" functions would have to be more "friend"-ly with "any" if you
do get rid of the other access methods. However, some compiler won't accept
template friends and/or template methods, so you may need to work around
- You could add a form of "any_cast" that allows a reference, especially
non-const ones, to the held object. Is there a way for "any_cast" to work
if the destination type isn't the stored object's type, but the object can
be converted to the destination type?
- I never really liked the "operator const void *" hack to get around the
over-eagerness of "operator bool" to be used in inappropriate integral
conversions. An alternate way around the problem would be to use "operator
bool," and declare, but leave undefined, conversion operators for all the
other integral types in a private section. (This is similar to how
"noncopyable" kills the copy constructor and assignment operator.) Maybe I
should add a class for this.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk