Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-01-30 15:26:44


On Wednesday 30 January 2002 03:08 pm, you wrote:
> > Also, I think it was a Scott Meyers' article that discussed not being
> > able to use standard algorithms in functions like ptr_fun because they
> > may include additional default parameters, which ptr_fun may not be
> > aware of (I hope I'm getting this right). It is hard for me by looking
> > at the code to make certain (compounded by the fact I can't find the
> > article right now) so would boost::bind be able to be a workaround for
> > such a problem?
>
> Unfortunately not. Once a function converts to a function pointer, there is
> no way to determine the default arguments, if any.
>
> The only way to do it portably is to write a forwarding function. I don't
> like it but this is what the standard says.
>
> Some compilers "remember" the default arguments and allow such uses but
> this is non-standard (and may be classified as a bug.)

Such compilers are doomed to fail in this, because default arguments can't be
carried along in type information. Try this little example on your favorite
compiler:

#include <iostream>

template<typename F>
void foo(F f)
{
  f(1);
}

void bar(int i, int j = 5, int k = 7)
{
  std::cout << "bar(" << i << ", " << j << ", " << k << ")\n";
}

void wibble(int i, int j = 6, int k = 8)
{
  std::cout << "wibble(" << i << ", " << j << ", " << k << ")\n";
}

int main()
{
  foo(&bar);
  foo(&wibble);
  foo(&bar);
  return 0;
}

This happily compiles on quite a few compilers, in the strictest of modes,
but the output is never correct :)

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk