Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::bind2nd

 

std::bind2nd is deprecated [2]. The function template std::bind provides a better solution [2].

 

std::bind2nd is a binder to call a binary function with the value bound as second argument. If the first argument needs to be bound, use std::bind1st instead.

 

The distinction between std::bind1st and std::bind2nd gets clear in division:

 

 

 

 

 

#include <algorithm>
#include <numeric>
#include <vector>

//From http://www.richelbilderbeek.nl/CppHalve.htm
void Halve (std::vector<double>& v)
{
  std::transform(
    v.begin(),v.end(),v.begin(),
    std::bind2nd(std::divides<double>(),2.0));
}

 

 

 

 

 

Advice

 

 

 

 

 

 

References

 

  1. Scott Meyers. C++ And Beyond 2012 session: 'Initial thoughts on Effective C++11'. 2012. 'Prefer Lambdas over Binders'
  2. Working Draft, Standard for Programming Language C++. 2014-08-22. N3936. Paragraph D.10. 'The binders binder1st, bind1st and bind2nd are deprecated [Note: the function template bind provides a better solution -end note]'

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict