Boost logo

Boost Users :

Subject: Re: [Boost-users] Trouble with template on boost::function and boost::bind
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2012-03-11 05:03:05


On Sat, Mar 10, 2012 at 11:56 PM, pwx <airyai_at_[hidden]> wrote:

> I tried to create a template function that receives boost::function as
> parameter. Say like this:
>
> template <typename T1, typename T2>
> void proc(const boost::function<void(const T1&, T2&)> &p) {
> // ...
> }
>
> void f(const string& s1, string&s2) {
> // ...
> }
>
> Everything goes ok when I called proc( boost::function<void(const
> string&, string&)>(f) ). But when I try this:
>
> proc(boost::bind(f, _1, _2));
>
> The compiler refused with the error:
>
> no matching function for call to
> ‘proc(boost::_bi::bind_t<boost::_bi::unspecified, F,
> boost::_bi::list2<boost::arg<1>, boost::arg<2> > >)’
>
> Anyway to fulfill this design purpose? Thanks.
>

Explicitly provide the template parameters? E.g.,

proc< std::string, std::string >(boost::bind(f, _1, _2));

There's really no way for the compiler to figure out what T1 and T2 should
be without your help. Or maybe your proc should really be declared like

template< class F > void proc(F const & f);

?

Or maybe you can provide the above overload of proc and have its body
provide the explicit template parameters to the original overload of proc
for you?

Just some ideas.

- Jeff



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net