1

Boost.org's example given for fusion::transform is as follows:

struct triple
{
    typedef int result_type;

    int operator()(int t) const
    {
        return t * 3;
    };
};
// ...
assert(transform(make_vector(1,2,3), triple()) == make_vector(3,6,9));

Yet I'm not "getting it." The vector in their example contains elements all of the same type, but a major point of using fusion is containers of heterogeneous types. What if they had used make_vector(1, 'a', "howdy") instead?

int operator()(int t)
would need to become
template<typename T> T& operator()(T& const t)

But how would I write the result_type? template<typename T> typedef T& result_type certainly isn't valid syntax, and it wouldn't make sense even if it was, because it's not tied to the function.

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
Kyle
  • 4,487
  • 3
  • 29
  • 45
  • 1
    Are you sure that the result_type is required, rather than because the guy likes to put it in there? In the example above, he appears to be using a primitive form of decltype to form the result type. – Puppy May 15 '10 at 23:46
  • ugh, you're right, it's not required. Thanks. I'm going to post a new question asking for a real guide to boost::fusion, this documentation on boost.org is barely a step above reading the code itself.. Grrr. – Kyle May 16 '10 at 00:25

2 Answers2

5

Usually, fusion::transform is used with a templated (or -as shown above- otherwise overloaded) function operator:

struct triple 
{ 
    template <typename Sig>
    struct result;

    template <typename This, typename T>
    struct result<This(T)>
    {
        typedef /*...figure out return type...*/ type;
    };

    template <typename T> 
    typename result<triple(T)>::type 
    operator()(T t) const 
    { 
        return 3*t;    // relies on existing operator*() for 'T'
    }
}; 

And, additional sources of information about Fusion are the examples and the test directory where you can find demonstrations of some of the techniques.

Regards Hartmut

hkaiser
  • 11,403
  • 1
  • 30
  • 35
0

Have you tried overloading the call operator()?

struct triple
{

    int operator()(int t) const
    {
        return t * 3;
    };
    int operator()(string t) const
    {
        return t + t + t;
    };
};
Vicente Botet Escriba
  • 4,305
  • 1
  • 25
  • 39