C++ 在std::for_内部使用std::bind

C++ 在std::for_内部使用std::bind,c++,c++11,std,stdbind,C++,C++11,Std,Stdbind,考虑以下代码段: #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.

考虑以下代码段:

#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));
}
#包括
#包括
#包括
#包括
#包括
typedef std::string::size_type StrSize;
//从传递的源字符串的开始和结束处剥离传递的符号。
空条(标准::字符串和源,常量字符*符号)
{
strize v_start=源。首先查找(符号)的(而不是);
源。擦除(0,v_开始);
StrSize v_end=源。查找(符号)的最后一个(非);
if(v_end<(source.size()-1))
{
源。擦除(v_end+1);
}
}
///从容器中所有字符串的开始和结束处去除符号。
模板
空条(T和源、常量字符*符号)
{
对于每个(source.begin()、source.end()、std::bind(Strip、std::占位符::_1、符号));
}
编译时使用:

g++ -g -std=c++11 -c <filename>.cpp
g++-g-std=c++11-c.cpp
它会产生以下错误:

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));
testBInd.cpp:在函数“void Strip(T&,const char*)”中:
testBInd.cpp:26:90:错误:调用“bind(,const std::_Placeholder&,const char*&)”时没有匹配的函数
对于每个(source.begin()、source.end()、std::bind(Strip、std::占位符::_1、符号));
^
testBInd.cpp:26:90:注:候选人包括:
在/usr/include/c++/4.8.2/memory:79:0中包含的文件中,
来自testBInd.cpp:3:
/usr/include/c++/4.8.2/functional:1655:5:注意:模板类型名称std::_Bind_helper::type std::Bind(_Func&,_BoundArgs&&…)
绑定(_Func&&u f,_BoundArgs&&…_参数)
^
/usr/include/c++/4.8.2/functional:1655:5:注意:模板参数推断/替换失败:
testBInd.cpp:26:90:注意:无法推断模板参数“\u Func”
对于每个(source.begin()、source.end()、std::bind(Strip、std::占位符::_1、符号));
^
在/usr/include/c++/4.8.2/memory:79:0中包含的文件中,
来自testBInd.cpp:3:
/usr/include/c++/4.8.2/functional:1682:5:注意:模板类型名称std::\u Bindres\u helper::type std::bind(\u Func&,\u BoundArgs&&…)
绑定(_Func&&u f,_BoundArgs&&…_参数)
^
/usr/include/c++/4.8.2/functional:1682:5:注意:模板参数推断/替换失败:
testBInd.cpp:26:90:注意:无法推断模板参数“\u Result”
对于每个(source.begin()、source.end()、std::bind(Strip、std::占位符::_1、符号));

你能解释一下我错在哪里吗?为什么找不到匹配的呼叫

编译器无法决定使用哪个重载函数,如果只指定名称, 可能的解决方案:

std::for_each(source.begin(), source.end(), std::bind(
              static_cast<void (*)(std::string&, const char *)>(Strip),
              std::placeholders::_1, symbol));
std::for_each(source.begin()、source.end()、std::bind(
静态铸件(带材),
std::占位符::1,符号));