Boost logo

Boost :

From: rwgk (rwgk_at_[hidden])
Date: 2002-02-25 18:29:09

The attached code is a simplified fragment from a package
for numerics with arrays.

There is a scalar function foo() (think foo() is a function
declared in <cmath>) and an associated function
array_foo(). Using boost::bind() a pointer to foo() is
passed to a generic array_unary_function() which loops over
the array and applies the passed function to each element.

This works if there is just one foo() but fails if there
are several overloads for foo() (remove the // in line 4
of the code to see the error).

Is it possible to select the correct function in this
context, i.e. without knowing the exact argument or
return type? E.g.

  boost::bind(select_given_arg_type<ElementType>(foo), _1)

My best idea right now is to #define the generic function.

Thanks for any hints,

P.S.: Eventually there will be two versions of the generic
array_unary_function(), selected via tag dispatching
involving boost::has_trivial_destructor.

#include <vector>
#include <boost/bind.hpp>

//int foo(int x) { return 2 * x; }
double foo(double x) { return 3. * x; }

template <typename BoundUnaryFunctionType,
          typename ElementType>
void array_unary_function(BoundUnaryFunctionType f,
                          ElementType* a_begin,
                          const std::size_t& a_sz)
  for(std::size_t i=0;i<a_sz;i++) {
    a_begin[i] = f(a_begin[i]);

template <typename ElementType>
void array_foo(std::vector<ElementType>& a)
    boost::bind(foo, _1),

int main()
  std::vector<float> a(5);
  return 0;

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