以下代码:
$ cat test02.cpp
#include <string>
#include <numeric>
#include <cstdlib>
#include <list>
#include <iostream>
struct myadd :
public std::binary_function
<const std::string&,const std::string&,std::string>
{
std::string operator () (const std::string& x,const std::string& y) const {
return x+" "+y;
}
};
struct mymul :
public std::binary_function
<const std::string&,const std::string&,std::string>
{
std::string operator () (const std::string& x,const std::string& y) const {
return x+y;
}
};
std::string spliceme(
const std::list <std::string>& list1,
const std::list <std::string>& list2,
const std::binary_function
<const std::string&,const std::string&,std::string>& add,
const std::binary_function
<const std::string&,const std::string&,std::string>& mul
) {
return std::inner_product(list1.cbegin(),list1.cend(),list2.cbegin(),
std::string(""),add,mul);
}
int main() {
std::list <std::string> list1;
list1.emplace_back("First");
list1.emplace_back("Second");
list1.emplace_back("Third");
std::list <std::string> list2;
list2.emplace_back("Foerst");
list2.emplace_back("Annen");
list2.emplace_back("Tredje");
std::string result = spliceme(list1,list2,myadd(),mymul());
std::cout << result << std::endl;
return EXIT_SUCCESS;
}
生成编译器错误:
g++ -std=c++0x test02.cpp -o test02
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.6.3/include/g++-v4/numeric:62:0,
from test02.cpp:2:
/usr/lib/gcc/i686-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_numeric.h: In function '_Tp std::inner_product(_InputIterator1, _InputIterator1, _InputIterator2, _Tp, _BinaryOperation1, _BinaryOperation2) [with _InputIterator1 = std::_List_const_iterator<std::basic_string<char> >, _InputIterator2 = std::_List_const_iterator<std::basic_string<char> >, _Tp = std::basic_string<char>, _BinaryOperation1 = myadd, _BinaryOperation2 = std::binary_function<const std::basic_string<char>&, const std::basic_string<char>&, std::basic_string<char> >]':
test02.cpp:29:101: instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_numeric.h:218:2: error: no match for call to '(std::binary_function<const std::basic_string<char>&, const std::basic_string<char>&, std::basic_string<char> >) (const std::basic_string<char>&, const std::basic_string<char>&)'
make: *** [all] Error 1
问题出现在线路上:
return std::inner_product(list1.cbegin(),list1.cend(),list2.cbegin(),
std::string(""),add,mul);
如果我直接实例化类 myadd 和 mymul :
return std::inner_product(list1.cbegin(),list1.cend(),list2.cbegin(),
std::string(""),myadd(),mymul());
一切都编译并正确运行。我将函数 add 和 mul 传递到函数 spliceme 的方式有什么问题?