AutosarC++18_03-A18.9.1

The std::bind shall not be used

Required inputs: IR

Rationale

Using the std::bind function makes the function call less readable and may lead to the developer confusing one function parameter with another. Also, compilers are less likely to inline the functions that are created using std::bind.

It is recommended to use lambda expressions instead.

Example
// $Id: A18-9-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <functional>
class A {
  // Implementation
};
void Fn(A const &a, double y) noexcept {
  // Implementation
}
void F1() noexcept {
  double y = 0.0;
  auto function = std::bind(&Fn, std::placeholders::_1, y); // Non-compliant
  // ...
  A const a{};
  function(a);
}
void F2() noexcept {
  auto lambda = [](A const &a) -> void {
    double y = 0.0;
    Fn(a, y);
  }; // Compliant
  // ...
  A const a{};
  lambda(a);
}
See Also
  • Effective Modern C++ [12]: Item 34: Prefer lambdas to std::bind

Possible Messages

Key

Text

Severity

Disabled

forbidden_libheader_symbol_use

Usage of forbidden entity std::bind from <{}>.

None

False

Options

included_headers

included_headers : bool = True

Whether the rule should also look in headers included by the configured symbol_header.
 

symbol_header

symbol_header : set[str] = set()

Name of the system or user header files of which the symbols should not be used.
 

symbols

symbols : set[bauhaus.analysis.config.QualifiedName] = {'std::bind'}

Names of symbols which are forbidden.
 

system_symbol_header

system_symbol_header : set[str] = {'functional'}

Name of the system header files of which the symbols should not be used.
 

translate_header_name

translate_header_name : bool = False

Whether to auto-translate the symbol_header (e.g. stdlib{.h} -> cstdlib).
 

user_symbol_header

user_symbol_header : set[str] = set()

Name of the user header files of which the symbols should not be used.