Boost logo

Boost :

From: Daniel James (daniel_at_[hidden])
Date: 2004-11-18 09:00:40

I've written a macro (the implementation is attached) for generating a
function object for using overloaded functions with generic code such as
the STL algorithms and boost::bind. A simple example of it's use:

     void foo(int);
     void foo(char*);

     GENERATE_OVERLOADED(void, foo);

     int main()
         int values[] = { 1, 2, 3, 4 };
         std::for_each(values, values+4, foo_overloaded);

GENERATE_OVERLOADED (yes, the naming is pretty bad) creates an object
called which contains overloaded calls to operator() which forward to
the named functions. Something like:

     struct foo_overloaded_type
         void operator() const
             return foo();

         template <class T1>
         void operator(T1 const& p1) const
             return foo(p1);

         template <class T1, class T2>
         void operator(T1 const& p1, T2 const& p2) const
             return foo(p1, p2);

         // etc.
     } foo_overloaded;

The main advantage is that you don't have to select the desired
overload. It can also be useful when different calls will have different
argument types such as iterating over a tuple in fusion (I guess you
could call it a visitor).

Any comments on this? Would anyone else find this useful? Also, has
anyone done something similar? I'd be surprised if no one has, I always
feel like I'm missing something when I'm doing this kind of thing.

The implementation is lacking in several ways - it doesn't work for
non-const references, the return type is fixed, needs to deal with
member functions, namespace issues and so on. I suppose, it could also
be better integrated with bind, lambda or pheonix.


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