Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) std::bind1st

 

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

 

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

 

The use of std::bind1st is clearest in division:

 

 

 

 

 

Example: Reciprocal

 

Reciprocal replaces all elements by their reprocicals, that is replaces all elements called 'x' by '1.0/x'.

 

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

//From http://www.richelbilderbeek.nl/CppReciprocal.htm
void Reciprocal(std::vector<double>& v)
{
  std::transform(v.begin(),v.end(),v.begin(),
    std::bind1st(std::divides<double>(),1.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

This page has been created by the tool CodeToHtml