Function functors?

Discussion in 'C++' started by mlt, Aug 28, 2008.

  1. mlt

    mlt Guest

    I have a function that computes the distance between some objects. The
    distance could be euclidian, manhaten etc. I would therefore like to
    implement the distance metric as a functor as seen below:

    template<typename dist>
    function compute_distance(double a, double b)
    {
    dist dist_measure;
    double distance = dist_measure(a,b);
    return distance;
    }


    But can 'dist' just be a separate function or should be a class? Since its
    use as a template parameter I assume it must be a type/class and cannot be a
    simple function.
    mlt, Aug 28, 2008
    #1
    1. Advertising

  2. mlt

    Kai-Uwe Bux Guest

    mlt wrote:

    > I have a function that computes the distance between some objects. The
    > distance could be euclidian, manhaten etc. I would therefore like to
    > implement the distance metric as a functor as seen below:
    >
    > template<typename dist>
    > function compute_distance(double a, double b)
    > {
    > dist dist_measure;
    > double distance = dist_measure(a,b);
    > return distance;
    > }
    >
    >
    > But can 'dist' just be a separate function or should be a class? Since its
    > use as a template parameter I assume it must be a type/class and cannot be
    > a simple function.


    The way you set it up, the template parameter has to be a class.

    a) I do not exactly see what the above function buys you. You would have to
    invoke it like so:

    compute_distance< dist >( a, b )

    which is just marginally better than

    dist()( a, b )

    and worse than

    dist( a, b )


    b) To answer your technical question: you can use a function as a template
    parameter:

    template < double (dist) ( double, double ) >
    double f ( double a, double b ) {
    return ( dist(a,b) );
    }

    double p1 ( double a, double b ) {
    return ( a );
    }

    #include <iostream>

    int main ( void ) {
    std::cout << f<p1>( 0.1, 0.2 ) << '\n';
    }


    However, that does not buy you anything either. What is the advantage of the
    more convoluted:

    f<p1>( a, b )

    over:

    p1( a, b )




    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Aug 28, 2008
    #2
    1. Advertising

  3. mlt

    mlt Guest

    "Kai-Uwe Bux" <> skrev i en meddelelse
    news:g97352$fhj$...
    > mlt wrote:
    >
    >> I have a function that computes the distance between some objects. The
    >> distance could be euclidian, manhaten etc. I would therefore like to
    >> implement the distance metric as a functor as seen below:
    >>
    >> template<typename dist>
    >> function compute_distance(double a, double b)
    >> {
    >> dist dist_measure;
    >> double distance = dist_measure(a,b);
    >> return distance;
    >> }
    >>
    >>
    >> But can 'dist' just be a separate function or should be a class? Since
    >> its
    >> use as a template parameter I assume it must be a type/class and cannot
    >> be
    >> a simple function.

    >
    > The way you set it up, the template parameter has to be a class.
    >
    > a) I do not exactly see what the above function buys you. You would have
    > to
    > invoke it like so:
    >
    > compute_distance< dist >( a, b )
    >
    > which is just marginally better than
    >
    > dist()( a, b )
    >
    > and worse than
    >
    > dist( a, b )



    The point is that a lot more things has to be done in compute_distance and
    calculating distance between a and b should be used in later computations in
    compute_distance. Based on this and the fact that it should be possible to
    change the metric for dist(a,b) I cannot think of a better way than to use a
    functor.
    mlt, Aug 28, 2008
    #3
  4. mlt

    Kai-Uwe Bux Guest

    mlt wrote:

    >
    > "Kai-Uwe Bux" <> skrev i en meddelelse
    > news:g97352$fhj$...
    >> mlt wrote:
    >>
    >>> I have a function that computes the distance between some objects. The
    >>> distance could be euclidian, manhaten etc. I would therefore like to
    >>> implement the distance metric as a functor as seen below:
    >>>
    >>> template<typename dist>
    >>> function compute_distance(double a, double b)
    >>> {
    >>> dist dist_measure;
    >>> double distance = dist_measure(a,b);
    >>> return distance;
    >>> }
    >>>
    >>>
    >>> But can 'dist' just be a separate function or should be a class? Since
    >>> its
    >>> use as a template parameter I assume it must be a type/class and cannot
    >>> be
    >>> a simple function.

    >>
    >> The way you set it up, the template parameter has to be a class.
    >>
    >> a) I do not exactly see what the above function buys you. You would have
    >> to
    >> invoke it like so:
    >>
    >> compute_distance< dist >( a, b )
    >>
    >> which is just marginally better than
    >>
    >> dist()( a, b )
    >>
    >> and worse than
    >>
    >> dist( a, b )

    >
    >
    > The point is that a lot more things has to be done in compute_distance and
    > calculating distance between a and b should be used in later computations
    > in compute_distance. Based on this and the fact that it should be possible
    > to change the metric for dist(a,b) I cannot think of a better way than to
    > use a functor.


    In that case, you might consider the following signature:

    template < typename Func >
    some_type do_stuff ( double a, double b, Func dist ) {
    ...
    ... dist( a, b ) ...;
    ...
    }

    This is most flexible: when you write

    do_stuff( a, b, euclidean_dist );

    the parameter euclidean_dist could be a function or a functor object. Its
    type will be deduced.



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Aug 28, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul MG
    Replies:
    2
    Views:
    383
    Dhruv
    Jul 3, 2003
  2. Rob Williscroft

    Re: Need help with generic functors....

    Rob Williscroft, Aug 17, 2003, in forum: C++
    Replies:
    3
    Views:
    341
    Gordon Scott
    Aug 18, 2003
  3. red floyd
    Replies:
    0
    Views:
    313
    red floyd
    Nov 13, 2003
  4. leaf
    Replies:
    2
    Views:
    298
  5. Martin Herbert Dietze

    Loki-like functors with boost::function?

    Martin Herbert Dietze, Mar 30, 2006, in forum: C++
    Replies:
    2
    Views:
    468
    Martin Herbert Dietze
    Mar 30, 2006
Loading...

Share This Page