Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem converting template function argument to boost::function

Reply
Thread Tools

Problem converting template function argument to boost::function

 
 
eefacm@gmail.com
Guest
Posts: n/a
 
      01-24-2008
I've been experimenting with Boost.Function. I wrote a little test
program, of which this is the guts:

template <typename T>
void call_with_arg(function<void (T)> func, T arg) {
func(arg);
}

void print_it(int x) {
cout << "printit: " << x << endl;
}

int main() {
call_with_arg(print_it, 1);
}

When I compile this with g++ 4.0.1, I get this error:

a.cc: In function 'int main()':
a.cc:24: error: no matching function for call to 'call_with_arg(void
(&)(int), int)'

I can't figure out why. It works if I say:

call_with_arg<int>(print_it, 1);

Or:

function<void (int)> func = print_it;
call_with_arg(func, 1);

So why doesn't the first way work?
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      01-25-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I've been experimenting with Boost.Function. I wrote a little test
> program, of which this is the guts:
>
> template <typename T>
> void call_with_arg(function<void (T)> func, T arg) {
> func(arg);
> }
>
> void print_it(int x) {
> cout << "printit: " << x << endl;
> }
>
> int main() {
> call_with_arg(print_it, 1);
> }
>
> When I compile this with g++ 4.0.1, I get this error:
>
> a.cc: In function 'int main()':
> a.cc:24: error: no matching function for call to 'call_with_arg(void
> (&)(int), int)'
>
> I can't figure out why. It works if I say:
>
> call_with_arg<int>(print_it, 1);
>
> Or:
>
> function<void (int)> func = print_it;
> call_with_arg(func, 1);
>
> So why doesn't the first way work?


Because conversion isn't involved for function template argument deduction.

call_with_arg(print_it, 1); needs to convert print_it into
function<void(int)>.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointer as template argument - does not work for function template avasilev C++ 4 12-22-2011 02:11 PM
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
Template function as argument to another template function Jim West C++ 3 10-07-2004 07:21 PM



Advertisments