Topic: std: should this work?


Author: fleye@my-deja.com
Date: 1999/09/21
Raw View
#include <stdio.h>

void f0(int) { puts("f0"); }
void f1(const int&) { puts("f1"); }

template <typename T> void Fun(void (*f)(T)) { f(0); }

int main()
{
  Fun(f0);      // this works of course.
  Fun(f1);      // should this work?
  return 0;
}

My compiler barfs at Fun(f1) complaining about passing `void (*)(const
int&)' to 'void (*)(int)'. I thought T should be `const int&' in the
second case. My current work around is to provide overloaded Funs. I
browsed the dec96pub wp and still feel fuzzy about this. Could some std
guy/langlawyer clarify this for me. Thanks in advance.

Fly.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Darin Adler" <darin@bentspoon.com>
Date: 1999/09/22
Raw View
fleye@my-deja.com wrote:

> #include <stdio.h>
>
> void f0(int) { puts("f0"); }
> void f1(const int&) { puts("f1"); }
>
> template <typename T> void Fun(void (*f)(T)) { f(0); }
>
> int main()
> {
>   Fun(f0);      // this works of course.
>   Fun(f1);      // should this work?
>   return 0;
> }
>
> My compiler barfs at Fun(f1) complaining about passing `void (*)(const
> int&)' to 'void (*)(int)'. I thought T should be `const int&' in the
> second case. My current work around is to provide overloaded Funs. I
> browsed the dec96pub wp and still feel fuzzy about this. Could some std
> guy/langlawyer clarify this for me. Thanks in advance.

I read the section on template argument deduction. It seems that both cases
should work.

Then I tried it with my favorite compiler. It worked fine.

    -- Darin


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]