Chani
Chani

Reputation: 5165

Using std::bind inside std::for_each

Consider the following snippet:

#include <string>
#include <map>
#include <memory>
#include <utility>
#include <iostream>

typedef std::string::size_type StrSize;

//Strips the passed symbol from the start and end of the passed source string.
void Strip(std::string& source, const char* symbol)
{
    StrSize v_start = source.find_first_not_of(symbol);
    source.erase(0, v_start);

    StrSize v_end = source.find_last_not_of(symbol);
    if (v_end < (source.size() - 1))
    {
        source.erase(v_end + 1);
    }
}

///Strips the symbol from the start and end of all strings in the container.
template <class T>
void Strip(T& source, const char* symbol)
{
    for_each(source.begin(), source.end(), std::bind(Strip, std::placeholders::_1, symbol));
}

Compile using:

g++ -g -std=c++11 -c <filename>.cpp

It yields the following error:

testBInd.cpp: In function ‘void Strip(T&, const char*)’:
testBInd.cpp:26:90: error: no matching function for call to ‘bind(<unresolved overloaded function type>, const std::_Placeholder<1>&, const char*&)’
     for_each(source.begin(), source.end(), std::bind(Strip, std::placeholders::_1, symbol));
                                                                                          ^
testBInd.cpp:26:90: note: candidates are:
In file included from /usr/include/c++/4.8.2/memory:79:0,
                 from testBInd.cpp:3:
/usr/include/c++/4.8.2/functional:1655:5: note: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__or_<std::is_integral<typename std::decay<_Tp>::type>, std::is_enum<typename std::decay<_Tp>::type> >::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/usr/include/c++/4.8.2/functional:1655:5: note:   template argument deduction/substitution failed:
testBInd.cpp:26:90: note:   couldn't deduce template parameter ‘_Func’
     for_each(source.begin(), source.end(), std::bind(Strip, std::placeholders::_1, symbol));
                                                                                          ^
In file included from /usr/include/c++/4.8.2/memory:79:0,
                 from testBInd.cpp:3:
/usr/include/c++/4.8.2/functional:1682:5: note: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/usr/include/c++/4.8.2/functional:1682:5: note:   template argument deduction/substitution failed:
testBInd.cpp:26:90: note:   couldn't deduce template parameter ‘_Result’
     for_each(source.begin(), source.end(), std::bind(Strip, std::placeholders::_1, symbol));

Can you explain where I am wrong? Why is it unable to find a matching call?

Upvotes: 1

Views: 1452

Answers (1)

fghj
fghj

Reputation: 9394

Compiler can not decide which overload function to use, if you specify only name, possible fix:

std::for_each(source.begin(), source.end(), std::bind(
              static_cast<void (*)(std::string&, const char *)>(Strip),
              std::placeholders::_1, symbol));

Upvotes: 3

Related Questions