Boost logo

Boost :

From: Steven Watanabe (steven_at_[hidden])
Date: 2006-09-02 16:42:32


AMDG

Alexander Nasonov,

    Instead of using anyT as the placeholder I think that creating
separate placeholders for each argument may be better. It allows
for better checking at compile time when the argument types really
model different concepts.

struct to_ostream;
struct ostream : function_arg<to_ostream, 0> {};
struct ostreamable : function_arg<to_ostream, 1> {};
struct to_ostream : function<ostream&(ostream&, ostreamable&)> {
    template<class R, class Os, class T>
    R execute(Os& os, T& t) {
       return(os << t);
    }
};
void f() {
    any<ostream> os(ref(cout));
    any<ostreamable> value(1);
    to_ostream()(os, value);
}

In order to make this work the function pointers have to take
arguments of a type that stores only enough information to
convert it to the real type. Basically it would be boost::any
with direct support for reference_wrappers added. This would
also mean that the function pointers for a given operation are all
identical regardless of the number of other functions. Thus, "up-casting"
anys will work.

The main disadvantage of this scheme appears when one or more arguments
are not casted. This could happen with lambda expressions. The solution is
to give the argument enough information to reconstruct the original type or
a subset thereof.

When the return type is an any the same method can be used to avoid making
the return types different for different sets of operations and thereby
disabling
conversions. Additionally, the tables required can be used to implement
"down-casts."

In Christ,
Steven Watanabe


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