> Indika Bandara <[EMAIL PROTECTED]> wrote:
> > > > for example;
> > > > typedef void (*foo)(int, ...);
> > > > void func1(int, ...);
> > > > void func2(int, float);
> > > >
> > > > foo f1 = func1; // OK
> > > > foo f2 = func2; // NOT OK
> >
> > why doesn't it compile?

Because it violates a language constraint.

There is no _implicit_ conversion from one function pointer type
to another.

> > since type foo can hold arguments more than 1
> > isn't it supposed to support func2?

No.

"Paul Herring" <[EMAIL PROTECTED]> wrote:
> foo can only hold a function with 2 'arguments'
> which are an int, and a varadic list.

That isn't strictly true. Any function pointer can be converted
to another function pointer type and back again without loss.
The conversion must be done explicitly though. This doesn't mean
that you can call a function with the wrong signature though.
[cf 6.3.2.3p8]

> Since float is not compatible with a varadic list, it
> cannot be assigned the value of func2.

It can with a cast...

  foo f1 = func1;
  foo f2 = (foo) func2;

But before you can call func2 through f2, you need to convert
f2 back to the appropriate signature...

  ((void (*)(int, float)) f2)(42, 3.14159);

Or, slightly more clearly...

  typedef void (*func2_t)(int, float);
  func2_t f = (func2_t) f2;
  f(42, 3.14159);

Note that some people choose not to hide the pointer indirection
in such typedefs...

  typedef void func2_t(int, float);
  func2_t *f = (func2_t *) f2;
  f(42, 3.14159);

> (Details in the Standard[tm] aside, it has to do with how the
> compiler deals with the varadic list<snip>

In other words, variadic functions are different beasts to
ordinary functions. Whilst function pointers can be converted,
the calling conventions may differ.

-- 
Peter

Reply via email to