Thread: functional derivation better?

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    functional derivation better?

    I'm told that if I derive my comparison function objects from std::binary_function, I'm "helping the STL." How? What does, std::sort(), for example, deduce from that template? Why is
    Code:
    class comp_square : public std::binary_function<double,double,bool>
    {
         bool operator() (const double & left, const double & right)
         {
                return std::pow(left,2) < std::pow(right,2);
         }
    
         // . . . . . . 
    };
    better than

    Code:
    class comp_square
    {
         bool operator() (const double & left, const double & right)
         {
                return std::pow(left,2) < std::pow(right,2);
         }
    
         // . . . . . . 
    };
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    In that example, there is no difference at all, reason is... std::binary_function is never used...

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CodeMonkey View Post
    I'm told that if I derive my comparison function objects from std::binary_function, I'm "helping the STL." How? What does, std::sort(), for example, deduce from that template?
    It gives the typedefs: first_argument_type, second_argument_type, result_type.

    In theory, the STL (or your own algorithms even) might have some partial or complete specialization of an algorithm based on the specific types being passed to a functor. This information COULD be carried around as template type parameters, but this is unwieldy. It would force the user programmer to explicitly indicate the types for every instantiation of an object which acts like a binary function, even though the vast majority of algorithms don't make use of this information.


    You also need the result_type if you want boost::bind to work correctly with your function object. You could define this yourself (and I have), but you could just as well derive from binary_function.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I see. Thank you.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dispose pattern in class derivation
    By George2 in forum C# Programming
    Replies: 5
    Last Post: 04-19-2008, 05:03 AM
  2. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  3. Functional programming languages... r they really functional?
    By code_mutant in forum C++ Programming
    Replies: 10
    Last Post: 02-25-2004, 05:29 AM
  4. functional dependencies
    By DMaxJ in forum C++ Programming
    Replies: 10
    Last Post: 10-23-2002, 07:07 AM
  5. Classes and Derivation
    By phatslug in forum C++ Programming
    Replies: 4
    Last Post: 06-02-2002, 09:19 PM