tags:

views:

77

answers:

1

Hello!

Is there any way to make boost::bind work with std::fill?

I tried the following, but it didn't work:

boost::bind(std::fill, x.begin(), x.end(), 1);
+11  A: 

The problem is that std::fill is a template function. Template functions don't really exist, so to say, until they're instantiated. You can't take the address of std::fill because it doesn't really exist; it's just a template for similar functions that use different types. If you provide the template parameters, it will refer to a specific instantiation of the template, and everything will be okay.

The std::fill function has two template parameters: ForwardIteratorType, which is the type of an iterator to the container, and DataType, which is the type that container holds. You need to provide both, so the compiler knows which instantiation of the std::fill template you want to use.

std::vector<int> x(10);
boost::bind(std::fill<std::vector<int>::iterator, int>, x.begin(), x.end(), 1);
dauphic
Sorry, I think I worded the first sentence incorrectly. It's not really that it can't deduce the template parameters, but I'm experiencing a loss of words right now. :(
dauphic
+1 You probably want to rewrite it in terms of the fact that `std::fill` is a template, and the argument to bind must be a particular instantiation.
David Rodríguez - dribeas
Thank you, that's what I was looking for.
dauphic