Jump to content
  • Advertisement
Sign in to follow this  
3dcgmodeling

what does binary_function come to play?

This topic is 4171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

template<class T> struct less : public binary_function<T, T, bool> { bool operator()(const T& x, const T& y) const; }; template<class Arg1, class Arg2, class Result> struct binary_function { typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Result result_type; }; it seem binary_function do nothing. and class less could be defined below: template<class T> struct less<T,T,bool>{ bool operator()(const T&x, const T& y) const; };

Share this post


Link to post
Share on other sites
Advertisement
binary_function provides the typedef's for first_argument_type, second_argument_type and result_type.

These typedef's are important for writing generic code that uses a generic binary function.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
binary_function provides the typedef's for first_argument_type, second_argument_type and result_type.

These typedef's are important for writing generic code that uses a generic binary function.


could you give a sample for that?

Share this post


Link to post
Share on other sites
http://www.google.com.au/search?hl=en&q=c%2B%2B+binary_function+example
http://msdn2.microsoft.com/en-us/library/23bzsffd(VS.80).aspx


//arguments are a "binary_test &", an "int" and return type is "float"
class binary_test : public binary_function<binary_test &,int,float>
{
public:
float value;
binary_test(){value=10.0;}
binary_test(float x){value=x;}
//instead of taking an "int", this takes a "second_argument_type" (typedef'd inside binary_function)
result_type operator<<(second_argument_type arg2);
};

//instead of returning a "float", this returns a "result_type" (typedef'd inside binary_function)
binary_test::result_type binary_test::operator<<(binary_test::second_argument_type arg2)
{
//here, "float" could be replaced with "typename binary_test::result_type"
value = (float)(((int)value) << arg2);
cout << "New value after shift is " << value << endl;
return value;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!