1 .. title:: clang-tidy - modernize-avoid-bind
6 The check finds uses of ``std::bind`` and ``boost::bind`` and replaces them
7 with lambdas. Lambdas will use value-capture unless reference capture is
8 explicitly requested with ``std::ref`` or ``boost::ref``.
10 It supports arbitrary callables including member functions, function objects,
11 and free functions, and all variations thereof. Anything that you can pass
12 to the first argument of ``bind`` should be diagnosable. Currently, the only
13 known case where a fix-it is unsupported is when the same placeholder is
14 specified multiple times in the parameter list.
20 int add(int x, int y) { return x + y; }
28 auto clj = std::bind(add, x, _1);
37 auto clj = [=](auto && arg1) { return add(x, arg1); };
40 ``std::bind`` can be hard to read and can result in larger object files and
41 binaries due to type information that will not be produced by equivalent
47 .. option:: PermissiveParameterList
49 If the option is set to `true`, the check will append ``auto&&...`` to the end
50 of every placeholder parameter list. Without this, it is possible for a fix-it
51 to perform an incorrect transformation in the case where the result of the ``bind``
52 is used in the context of a type erased functor such as ``std::function`` which
53 allows mismatched arguments. For example:
58 int add(int x, int y) { return x + y; }
60 std::function<int(int,int)> ignore_args = std::bind(add, 2, 2);
61 return ignore_args(3, 3);
64 is valid code, and returns `4`. The actual values passed to ``ignore_args`` are
65 simply ignored. Without ``PermissiveParameterList``, this would be transformed into
69 int add(int x, int y) { return x + y; }
71 std::function<int(int,int)> ignore_args = [] { return add(2, 2); }
72 return ignore_args(3, 3);
75 which will *not* compile, since the lambda does not contain an ``operator()``
76 that accepts 2 arguments. With permissive parameter list, it instead generates
80 int add(int x, int y) { return x + y; }
82 std::function<int(int,int)> ignore_args = [](auto&&...) { return add(2, 2); }
83 return ignore_args(3, 3);
88 This check requires using C++14 or higher to run.