0

So I have inherited some code and while updating it to C++17 I receive the error message:

error C2039: 'binary_function': is not a member of 'std'

The binary_function is used in two places. Fortunately both code segments seem to be very similar

struct CompareNodes : public std::binary_function<double, double, bool>
{
  bool operator()(BOARD_NODE_STRUCT *pNS1, BOARD_NODE_STRUCT *pNS2)
  {
    return (*pNS1) < (*pNS2);
  }
};


struct CompareNodes : public std::binary_function<NODE_STRUCT *, NODE_STRUCT *, bool>
{
  bool operator()(NODE_STRUCT *pNS1, NODE_STRUCT *pNS2)
  {
    return (pNS1->name.CompareNoCase(pNS2->name) < 0);
  }
};

I guess that I'm still a bit of a novice because I have not seen code like this before. How do I update this code to something that is equivalent?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Gary
  • 61
  • 1
  • 9
  • 1
    It's hard to say just like that. First simply try to get rid of inheritance and compile your code. If does not work you can introduce your own implementation of std::binary function by copying the implementation from before-C++17 std lib. – Dmitry May 07 '20 at 12:47
  • [This answer](https://stackoverflow.com/a/22387006/751579) explains why it (and other similar functions) were _removed_ and suggests alternatives. – davidbak May 07 '20 at 12:48
  • 1
    Possible duplicate of [Why have unary_function, binary_function been removed from C++11?](https://stackoverflow.com/questions/22386882/why-have-unary-function-binary-function-been-removed-from-c11). – davidbak May 07 '20 at 12:49
  • tl;dr: just remove the inheritance, you don't need it in C++17 (or even in C++11). But also those call operators should probably be `const`. – Barry May 07 '20 at 14:14
  • @davidbak I don't understand the answer provided at [Why have unary_function, binary_function been removed from C++11?](https://stackoverflow.com/questions/22386882/why-have-unary-function-binary-function-been-removed-from-c11) To be honest I don't understand what binary_function does so that makes it hard for me to understand the answer. – Gary May 07 '20 at 14:53
  • @Barry why did you close my question? I still don't feel like I have an answer I can understand an use. – Gary May 07 '20 at 14:53
  • 1
    @Gary Start by looking up what [`std::binary_function`](https://en.cppreference.com/w/cpp/utility/functional/binary_function) does. – Barry May 07 '20 at 15:22
  • I also added another duplicate candidate, which may help. – Barry May 07 '20 at 15:29
  • @Barry I'm trying here. I have taken my best guess at to what the new code should look like. struct CompareNodes { typedef double first_argument_type; typedef double second_argument_type; typedef bool result_type; bool operator()(BOARD_NODE_STRUCT *pNS1, BOARD_NODE_STRUCT *pNS2) { return (*pNS1) < (*pNS2); } }; – Gary May 07 '20 at 15:57
  • Lost my formatting. It makes it difficult to read. The code compiles, but I'm still not 100% certain it is the correct change. – Gary May 07 '20 at 15:59
  • So those functions were removed because there are alternatives now. And the only reason binary_function existed was to define certain types for your class that inherited from it that some other functions, also now removed!, used to require. ([The answer I pointed to](https://stackoverflow.com/a/22387805/751579) explains that.) So ... follow @Dmitry 's suggestion above and just remove the parent class of binary_function and see what happens! Apparently you've already discovered it compiles correctly ... so you're done! – davidbak May 07 '20 at 16:19
  • @Gary As I said earlier, and as mentioned in one of the linked answer, just remove the inheritance. You don't need those type aliases (whether inheriting them or manually providing them). – Barry May 07 '20 at 16:39
  • I commented out the typedefs and it still compiles. Thank you everyone for walking me through this. – Gary May 07 '20 at 17:43

0 Answers0