1

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?

8
  • 3
    Which overload of std::min do you expect to call? Commented Jul 1 at 13:05
  • 1
    error means that it does not know how to compare iterator type with integer type Commented Jul 1 at 13:06
  • 5
    std::min_element is probably what you want. Commented Jul 1 at 13:06
  • 2
    Not sure of the purpose of errVal here... Commented Jul 1 at 13:07
  • 1
    min wants to determine the relative ordering of the iterators vecI.begin() and vecI.end() using the ordering function errVal. This doesn't work. You must have been looking for some other function. Commented Jul 1 at 13:21

2 Answers 2

4

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. .

Sign up to request clarification or add additional context in comments.

Comments

1

Translation

  • In the file stl_algobase.h, which is included from the file vector (at line 60), which in turn is included from coliru.cpp (at line 1),

  • an error occurred at the instantiation of the (template) function std::min(const _Tp&, const _Tp&, _Compare), where _Tp is an iterator type and _Compare of type int,

  • which happens at line 14, column 18, in the file coliru.cpp, and the reason is that the parameter __comp cannot be used as a function (the usage of __comp can be seen in line 281 - stl_algobase.h).

  • After all, the compilation terminated due to the compiler flag -Wfatal-errors.


If you follow the code (or the documentation), then you'll see, that __comp must be a comparison functor.

See also:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.