Problem converting template function argument to boost::function



 DEVELOP > c-Plus-Plus > Problem converting template function argument to boost::function

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

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


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER