求助:STL 算法为什么推不出所需要的重载的op函数

例如:

print是重载的,find_if既然需要的是一元谓词,为什么不能推出需要的接受一个参数的print? 

代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool print(int a)
{
    cout << "para 1:" << a << endl;
    return a > 0;
}

bool print(int a, int b)
{
    cout << "para 1:" << a << ", para 2:" << b << endl;
    return a > b;
}

int main()
{
    // 能推出重载的函数
    bool res = print(1);
    cout << "res:" << res << endl;

    vector<int> tes = {-1, -2, 1, 2};

    auto itbegin = tes.begin();
    // 为什么stl算法不能识别重载的函数?
    auto it = find_if(tes.begin(), tes.end(), print);
    
    // 指明类型就可以
    //auto it = find_if(tes.begin(), tes.end(), (bool (*)(int))(print));
    cout << "len:" << it - itbegin << endl;

    return 0;
}

编译报错如下:


> Executing task: C:\TDM-GCC-64\bin\g++.exe -g e:\Code\YiliyoProject\We_Code_CPP\sourcefile\test_tolower.cpp -o e:\Code\YiliyoProject\We_Code_CPP\sourcefile\test_tolower.exe <

e:\Code\YiliyoProject\We_Code_CPP\sourcefile\test_tolower.cpp: In function 'int main()':
e:\Code\YiliyoProject\We_Code_CPP\sourcefile\test_tolower.cpp:32:52: error: no matching function for call to 'find_if(std::vector<int>::iterator, std::vector<int>::iterator, <unresolved overloaded function type>)'
   32 |     auto it = find_if(tes.begin(), tes.end(), print);
      |                                                    ^
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/algorithm:62,
                 from e:\Code\YiliyoProject\We_Code_CPP\sourcefile\test_tolower.cpp:2:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/bits/stl_algo.h:3915:5: note: candidate: 'template<class _IIter, class _Predicate> _IIter std::find_if(_IIter, _IIter, _Predicate)'
 3915 |     find_if(_InputIterator __first, _InputIterator __last,
      |     ^~~~~~~
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/9.2.0/include/c++/bits/stl_algo.h:3915:5: note:   template argument deduction/substitution failed:
e:\Code\YiliyoProject\We_Code_CPP\sourcefile\test_tolower.cpp:32:52: note:   couldn't deduce template parameter '_Predicate'
   32 |     auto it = find_if(tes.begin(), tes.end(), print);
      |                                                    ^
终端进程已终止,退出代码: 1

注:此问题是对transform(s.begin(), s.end(), isalpha);编译报错的解释。

直接写isalpha会在std命名空间找到cctype和clocate两个文件中找到两个重载的isalpha函数。而写成::isalpha会在全局命名空间中找isalpha函数,全局命名空间中只有一个ctype.h中的isalpha函数,因此可以正确运行。

原因:模板实例化时,需要传确定类型的实参。

知识点:

(1)重载函数的指针

void ff(int*);

void ff(unsigned int);

void (*fp1)(unsigned int) = ff;

编译器通过指针类型决定选用那个函数,指针类型与重载函数中的某一个精确匹配。(根据目的的类型选择具体的)。

(2)模板类型的推断

根据实参的类型推断模板参数的类型。(根据源的类型确定目的得类型)。这与根据目的类型选择具体的重载函数矛盾。

sdghchj的回复:

那如果还有bool print(float a) 呢?
隐式调用模板是根据确定的模板实参(包括类型)推导生成出你想调用的模板函数,还没做到根据你想调用的模板函数来反推模板实参的具体类型那么智能。
auto it = find_if<vector<int>::iterator, bool(int)>(tes.begin(), tes.end(), print);
或者
auto it = find_if(tes.begin(), tes.end(), (bool(*)(int))&print);

或用lambda来写确定函数对象

auto it = find_if(tes.begin(), tes.end(),static_cast<bool (*)(int)>(print));

auto it = find_if(tes.begin(), tes.end(), [](int a) {return print(a); });

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值