Boost logo

Boost :

From: Steven Watanabe (steven_at_[hidden])
Date: 2007-10-21 21:38:43


Corrado Zoccolo <czoccolo <at>> writes:

> BTW, the idea of the hand made vtable born in the discussion with gpd. My
> first implementation used just one function ptr to encapsulate all the
> behaviour (with a signature to fit all the methods), and was substantially
> less efficient than the current solution.

You might find this useful if you want to generalize.
(I wrote it when I was trying to solve this same
problem a year ago)

Basic usage:

#include <boost/vtable/make.hpp>
#include <boost/vtable/pointer.hpp>

struct print_function {
    typedef void (*function_pointer_type)(const void*);
    template<class T>
    stuct apply {
        static void value(const void* x) {
            std::cout << *static_cast<const T*>(x);

    boost::vtable::make<boost::mpl::vector<print_function> >::type
> table_type;

table_type table;


double x = 2.5;
void* vp = &x;

table.find<function1>()(vp); // prints 2.5

In Christ,
Steven Watanabe

Boost list run by bdawes at, gregod at, cpdaniel at, john at