Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::greater not defined in MSVC2012

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

like image 702
Marco A. Avatar asked May 15 '13 14:05

Marco A.


2 Answers

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.

like image 97
Mike Seymour Avatar answered Sep 20 '22 00:09

Mike Seymour


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

like image 36
JBentley Avatar answered Sep 19 '22 00:09

JBentley