| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
24 Jan 2008 12:51:32 PM |
| Object: |
Problem converting template function argument to boost::function |
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?
.
|
|
| User: "Barry" |
|
| Title: Re: Problem converting template function argument to boost::function |
24 Jan 2008 07:30:10 PM |
|
|
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)>.
.
|
|
|
|

|
Related Articles |
|
|