Skip to content

Commit 251eb8f

Browse files
committed
Add note to 'never use std::bind'
1 parent 4ce45d6 commit 251eb8f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

08-Considering_Performance.md

+15
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,18 @@ std::cout << someThing() << '\n';
307307
This is very minor, but a `"\n"` has to be parsed by the compiler as a `const char *` which has to do a range check for `\0` when writing it to the stream (or appending to a string). A '\n' is known to be a single character and avoids many CPU instructions.
308308

309309
If used inefficiently very many times it might have an impact on your performance, but more importantly thinking about these two usage cases gets you thinking more about what the compiler and runtime has to do to execute your code.
310+
311+
312+
### Never Use `std::bind`
313+
314+
`std::bind` is almost always way more overhead (both compile time and runtime) than you need. Instead simply use a lambda.
315+
316+
```cpp
317+
// Bad Idea
318+
auto f = std::bind(&my_function, "hello", std::placeholders::_1);
319+
f("world");
320+
321+
// Good Idea
322+
auto f = [](const std::string &s) { return my_function("hello", s); };
323+
f("world")
324+
```

0 commit comments

Comments
 (0)