The test code is as shown below:
#include<vector>
#include<algorithm>
#include<iostream>
using std::vector;
using std::min;
using std::cout;
using std::endl;
int func()
{
vector<int>vecI = {1,2,3,4,5};
auto errVal = 0;
auto iter = min(vecI.begin(),vecI.end(),errVal);
cout << *iter << endl;
return 0;
}
int main() {
auto exit = (int (*)()) &func;
std::cout << exit() << std::endl;
}
As it should be evident, I am using an erroneous invocation of std::min
to generate the following compiler error:
g++ -c -o coliru.o coliru.cpp -ggdb -g3 -pedantic-errors -Wall -Wextra -Wfatal-errors -Wpedantic -std=c++20
In file included from /usr/include/c++/11/vector:60,
from coliru.cpp:1:
/usr/include/c++/11/bits/stl_algobase.h: In instantiation of ‘constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare) [with _Tp = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = int]’:
coliru.cpp:14:18: required from here
/usr/include/c++/11/bits/stl_algobase.h:281:17: error: ‘__comp’ cannot be used as a function
281 | if (__comp(__b, __a))
| ~~~~~~^~~~~~~~~~
compilation terminated due to -Wfatal-errors.
I am not sure I can comprehend what the compiler is trying to convey, based on the above error.
Can someone throw some light?
I am not sure I can comprehend what the compiler is trying to convey, based on the above error.
Can someone throw some light?
Start here.
In instantiation of ‘
constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)
[with _Tp = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = int]
’
That's the function you are calling. std::min(const _Tp&, const _Tp&, _Compare)
.
It's a function template, and you are instantiating this template with _Tp
as an iterator, and _Compare
as an int
. Specifically, you passed 0
.
error: ‘__comp’ cannot be used as a function
std::min
will use __comp
as a function and 0
is not usable as a function. The error is telling you this. .