Problem converting template function argument to boost::function

E

eefacm

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?
 
B

Barry

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)>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top