mirror of
https://github.com/cpp-best-practices/cppbestpractices.git
synced 2025-12-16 18:57:02 +03:00
Remove trailing whitespace
This commit is contained in:
@@ -11,5 +11,3 @@
|
||||
10. [Enable Scripting](10-Enable_Scripting.md)
|
||||
11. [Further Reading](11-Further_Reading.md)
|
||||
12. [Final Thoughts](12-Final_Thoughts.md)
|
||||
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ Use an industry standard widely accepted build tool. This prevents you from rein
|
||||
* [maiken](https://github.com/Dekken/maiken) - Crossplatform build tool with Maven-esque configuration style.
|
||||
* [Qt Build Suite](http://doc.qt.io/qbs/) - Crossplatform build tool From Qt.
|
||||
* [meson](http://mesonbuild.com/index.html) - Open source build system meant to be both extremely fast, and, even more importantly, as user friendly as possible.
|
||||
* [premake](https://premake.github.io/)
|
||||
* [premake](https://premake.github.io/)
|
||||
* [xmake](https://xmake.io) - A cross-platform build utility based on Lua. Modern C/C++ build tools, Support multi-language hybrid compilation
|
||||
|
||||
Remember, it's not just a build tool, it's also a programming language. Try to maintain good clean build scripts and follow the recommended practices for the tool you are using.
|
||||
@@ -340,7 +340,7 @@ MSVC's [Control Flow Guard](https://msdn.microsoft.com/en-us/library/windows/des
|
||||
|
||||
### Heap Profiling
|
||||
|
||||
* [Memoro](https://epfl-vlsc.github.io/memoro/) - A detailed heap profiler
|
||||
* [Memoro](https://epfl-vlsc.github.io/memoro/) - A detailed heap profiler
|
||||
|
||||
## Ignoring Warnings
|
||||
|
||||
@@ -410,6 +410,6 @@ Don't forget to make sure that your error handling is being tested and works pro
|
||||
|
||||
[pahole](https://linux.die.net/man/1/pahole) generates data on holes in the packing of data structures and classes in compiled code. It can also the size of structures and how they fit within the system's cache lines.
|
||||
|
||||
### BinSkim
|
||||
### BinSkim
|
||||
|
||||
[BinSkim](https://github.com/Microsoft/binskim) is a binary static analysis tool that provides security and correctness results for Windows Portable Executable and *nix ELF binary formats
|
||||
[BinSkim](https://github.com/Microsoft/binskim) is a binary static analysis tool that provides security and correctness results for Windows Portable Executable and *nix ELF binary formats
|
||||
|
||||
@@ -240,7 +240,7 @@ It also makes it possible to have two separate files next to each other on one s
|
||||
```
|
||||
|
||||
## Initialize Member Variables
|
||||
...with the member initializer list.
|
||||
...with the member initializer list.
|
||||
|
||||
For POD types, the performance of an initializer list is the same as manual initialization, but for other types there is a clear performance gain, see below.
|
||||
|
||||
@@ -288,8 +288,8 @@ private:
|
||||
};
|
||||
|
||||
// Good Idea
|
||||
// The default constructor for m_myOtherClass is never called here, so
|
||||
// there is a performance gain if MyOtherClass is not is_trivially_default_constructible.
|
||||
// The default constructor for m_myOtherClass is never called here, so
|
||||
// there is a performance gain if MyOtherClass is not is_trivially_default_constructible.
|
||||
class MyClass
|
||||
{
|
||||
public:
|
||||
@@ -454,4 +454,3 @@ The Rule of Zero states that you do not provide any of the functions that the co
|
||||
The goal is to let the compiler provide optimal versions that are automatically maintained when more member variables are added.
|
||||
|
||||
[This article](http://www.nirfriedman.com/2015/06/27/cpp-rule-of-zero/) provides a background and explains techniques for implementing nearly 100% of the time.
|
||||
|
||||
|
||||
@@ -34,9 +34,9 @@ public:
|
||||
* Always return by value.
|
||||
|
||||
|
||||
references: https://github.com/lefticus/cppbestpractices/issues/21 https://twitter.com/lefticus/status/635943577328095232
|
||||
references: https://github.com/lefticus/cppbestpractices/issues/21 https://twitter.com/lefticus/status/635943577328095232
|
||||
|
||||
### Do not pass and return simple types by const ref
|
||||
### Do not pass and return simple types by const ref
|
||||
|
||||
```cpp
|
||||
// Very Bad Idea
|
||||
@@ -47,7 +47,7 @@ public:
|
||||
: m_int_value(t_int_value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
const int& get_int_value() const
|
||||
{
|
||||
return m_int_value;
|
||||
@@ -69,7 +69,7 @@ public:
|
||||
: m_int_value(t_int_value)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int get_int_value() const
|
||||
{
|
||||
return m_int_value;
|
||||
@@ -101,7 +101,7 @@ auto mybuffer = std::make_unique<char[]>(length); // C++14
|
||||
auto mybuffer = std::unique_ptr<char[]>(new char[length]); // C++11
|
||||
|
||||
// or for reference counted objects
|
||||
auto myobj = std::make_shared<MyClass>();
|
||||
auto myobj = std::make_shared<MyClass>();
|
||||
|
||||
// ...
|
||||
// myobj is automatically freed for you whenever it is no longer used.
|
||||
@@ -111,7 +111,7 @@ auto myobj = std::make_shared<MyClass>();
|
||||
|
||||
Both of these guarantee contiguous memory layout of objects and can (and should) completely replace your usage of C-style arrays for many of the reasons listed for not using bare pointers.
|
||||
|
||||
Also, [avoid](http://stackoverflow.com/questions/3266443/can-you-use-a-shared-ptr-for-raii-of-c-style-arrays) using `std::shared_ptr` to hold an array.
|
||||
Also, [avoid](http://stackoverflow.com/questions/3266443/can-you-use-a-shared-ptr-for-raii-of-c-style-arrays) using `std::shared_ptr` to hold an array.
|
||||
|
||||
## Use Exceptions
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ Know and understand the existing C++ standard algorithms and put them to use.
|
||||
|
||||
* See [cppreference](https://en.cppreference.com/w/cpp/algorithm)
|
||||
* Watch [C++ Seasoning](https://www.youtube.com/watch?v=qH6sSOr-yk8)
|
||||
|
||||
|
||||
Consider a call to `[]` as a potential code smell, indicating that an algorithm was not used where it could have been.
|
||||
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ For more examples see [this article](http://blog2.emptycrate.com/content/templat
|
||||
|
||||
### Avoid Recursive Template Instantiations
|
||||
|
||||
Recursive template instantiations can result in a significant load on the compiler and more difficult to understand code.
|
||||
Recursive template instantiations can result in a significant load on the compiler and more difficult to understand code.
|
||||
|
||||
[Consider using variadic expansions and folds when possible instead.](http://articles.emptycrate.com/2016/05/14/folds_in_cpp11_ish.html)
|
||||
|
||||
@@ -289,11 +289,11 @@ if (MyObject obj(index); obj.good()) {
|
||||
|
||||
### Prefer `double` to `float`, But Test First
|
||||
|
||||
Depending on the situation and the compiler's ability to optimize, one may be faster over the other. Choosing `float` will result in lower precision and may be slower due to conversions. On vectorizable operations `float` may be faster if you are able to sacrifice precision.
|
||||
Depending on the situation and the compiler's ability to optimize, one may be faster over the other. Choosing `float` will result in lower precision and may be slower due to conversions. On vectorizable operations `float` may be faster if you are able to sacrifice precision.
|
||||
|
||||
`double` is the recommended default choice as it is the default type for floating point values in C++.
|
||||
|
||||
See this [stackoverflow](http://stackoverflow.com/questions/4584637/double-or-float-which-is-faster) discussion for some more information.
|
||||
See this [stackoverflow](http://stackoverflow.com/questions/4584637/double-or-float-which-is-faster) discussion for some more information.
|
||||
|
||||
### Prefer `++i` to `i++`
|
||||
... when it is semantically correct. Pre-increment is [faster](http://blog2.emptycrate.com/content/why-i-faster-i-c) than post-increment because it does not require a copy of the object to be made.
|
||||
@@ -313,7 +313,7 @@ for (int i = 0; i < 15; ++i)
|
||||
```
|
||||
|
||||
Even if many modern compilers will optimize these two loops to the same assembly code, it is still good practice to prefer `++i`. There is absolutely no reason not to and you can never be certain that your code will not pass a compiler that does not optimize this.
|
||||
You should be also aware that the compiler will not be able optimize this only for integer types and not necessarily for all iterator or other user defined types.
|
||||
You should be also aware that the compiler will not be able optimize this only for integer types and not necessarily for all iterator or other user defined types.
|
||||
The bottom line is that it is always easier and recommended to use the pre-increment operator if it is semantically identical to the post-increment operator.
|
||||
|
||||
### Char is a char, string is a string
|
||||
@@ -353,4 +353,3 @@ Properly use the already highly optimized components of the vendor provided stan
|
||||
#### `in_place_t` And Related
|
||||
|
||||
Be aware of how to use `in_place_t` and related tags for efficient creation of objects such as `std::tuple`, `std::any` and `std::variant`.
|
||||
|
||||
|
||||
@@ -26,5 +26,3 @@ Consider using a typesafe library like
|
||||
Note that stronger typing can also allow for more compiler optimizations.
|
||||
|
||||
* [Sorting in C vs C++](Sorting in C vs C++.pdf)
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
## C++
|
||||
|
||||
* https://github.com/isocpp/CppCoreGuidelines The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++
|
||||
* https://github.com/isocpp/CppCoreGuidelines The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++
|
||||
* https://www.gitbook.com/book/alexastva/the-ultimate-question-of-programming-refactoring-/details - The Ultimate Question of Programming, Refactoring, and Everything
|
||||
* http://llvm.org/docs/CodingStandards.html - LLVM Coding Standards - very well written
|
||||
* http://geosoft.no/development/cppstyle.html
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
# Final Thoughts
|
||||
|
||||
Expand your horizons and use other programming languages. Other languages have different constructs and expressions. Learning what else is out there will encourage you to be more creative with your C++ and write cleaner, more expressive code.
|
||||
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,2 +1,2 @@
|
||||
This work is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License.
|
||||
This work is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License.
|
||||
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/.
|
||||
|
||||
@@ -12,4 +12,3 @@
|
||||
* [Enable Scripting](10-Enable_Scripting.md)
|
||||
* [Further Reading](11-Further_Reading.md)
|
||||
* [Final Thoughts](12-Final_Thoughts.md)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user