std::greater not defined in MSVC2012

10,962

Solution 1

How come that std::greater is no more part of the std namespace in Visual Studio 2012?

I'd be very surprised if that were the case.

I now need to include <functional.h>

No, you need to include <functional> since that's the header that defines all the standard function objects, including greater.

I thought STL libraries remained the same across toolsets

They should be. But headers are allowed to include other headers, so sometimes you'll find that something is available even if you haven't included the correct header. But you can't rely on that, as you've found here. Always include all the headers you need for the things you use.

Solution 2

The following example, taken from here, compiles and executes correctly for me in Visual Studio 2012:

// greater example
#include <iostream>     // std::cout
#include <functional>   // std::greater
#include <algorithm>    // std::sort

int main () {
  int numbers[]={20,40,50,10,30};
  std::sort (numbers, numbers+5, std::greater<int>());
  for (int i=0; i<5; i++)
    std::cout << numbers[i] << ' ';
  std::cout << '\n';
  return 0;
}

Output:

50 40 30 20 10

As per the question comments and the example above, you need to include <functional>, not <functional.h>.

Share:
10,962
Marco A.
Author by

Marco A.

Former NVIDIA and AWS engineer. Don't take anything I say for granted: I'm always learning. Gold badge in the c++ tag Some of my favorite answers and questions gcc and clang implicitly instantiate template arguments during operator overload resolution In overload resolution, does selection of a function that uses the ambiguous conversion sequence necessarily result in the call being ill-formed? clang fails replacing a statement if it contains a macro c++11 constexpr flatten list of std::array into array Is a program compiled with -g gcc flag slower than the same program compiled without -g? How is clang able to steer C/C++ code optimization? Newton Raphson with SSE2 - can someone explain me these 3 lines Clang compilation works while gcc doesn't for diamond inheritance

Updated on June 12, 2022

Comments

  • Marco A.
    Marco A. almost 2 years

    How come that std::greater is no more part of the std namespace in Visual Studio 2012? I now need to include <functional>

    I thought STL libraries remained the same across toolsets