Size: 4560
Comment:
|
Size: 5094
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 2: | Line 2: |
See also: * [[CheatSheet/Gcc]] * [[VisualStudio]] |
|
Line 47: | Line 52: |
=== Thread safety analysis (TSA) === Documentation: * [[https://insights.sei.cmu.edu/sei_blog/2014/10/thread-safety-analysis-in-c-and-c.html|Thread Safety Analysis in C and C++]]: CMU blog article on TSA * [[https://reviews.llvm.org/D14731|Thread safety analysis support for libc++, annotating mutex, lock_guard, etc]] Example projects: * [[https://github.com/ClangBuiltLinux/thread-safety-analysis|ClangBuiltLinux/thread-safety-analysis]]: Linux kernel w/ TSA annotations * Chromium, annotation macros, from [[https://github.com/chromium/chromium/blob/master/base/thread_annotations.h|base/thread_annotations.h]]. Macros are named different, and they use deprecated TSA names. === Lifetime safety analysis === * [[https://github.com/mgehre/llvm-project|mgehre/llvm-project]]: clang implementation * note: gsl = Guidelines Support Library |
|
Line 48: | Line 70: |
* Namespacing enums in pre-C++11: https://stackoverflow.com/questions/482745/namespaces-for-enum-types-best-practices#484304 |
|
Line 91: | Line 115: |
== C++ move semantics/perfect forwarding == | == Dynamic loading of DLL/DSOs == |
Line 93: | Line 117: |
* [[http://thbecker.net/articles/rvalue_references/section_01.html|C++ Rvalue References Explained]] * [[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html#Move_Semantics|Rvalue Reference Quick Look]] * [[http://en.cppreference.com/w/cpp/utility/move|std::move - cppreference.com]] * [[https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/RVO_V_S_std_move?lang=en|RVO V.S. std::move (C/C++ Cafe)]] * [[http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html|Rvalue References and Move Semantics in C++11 - Cprogramming.com]] |
Example of loading OpenCL dynamically. Uses a Python parser script to create special header file with defines that map both functions and does appropriate type conversion. https://github.com/opencv/opencv/pull/1542/files |
Line 99: | Line 119: |
== C++ and Python == | [[http://www.boost.org/doc/libs/master/doc/html/boost_dll.html|Boost.DLL]]: Boost library for easily loading DLLs (e.g. for plugins). Cross platform, but requires runtime libraryes boost_system (integrated into C++11?) and boost_filesystem (integrated into C++17). |
Line 101: | Line 121: |
[[https://github.com/pybind/pybind11|pybind/pybind11: Seamless operability between C++11 and Python]] [[https://github.com/tbenthompson/cppimport|tbenthompson/cppimport: Import C++ files directly from Python!]] == Stuff == * [[https://github.com/fffaraz/awesome-cpp|awesome-cpp]]: A curated list of awesome C/C++ frameworks, libraries, resources, and shiny things. Inspired by awesome-... stuff. * [[https://www.conan.io/|Conan C/C++ Package Manager]] |
https://github.com/knusbaum/CPP-Dynamic-Class-Loading: Class for dynamically loading classes (Linux only) |
Contents
See also:
C++ style guides
Qt's Coding Style and Coding Conventions
isocpp/CppCoreGuidelines: The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++, from Bjarne Stroustrup and Herb Sutter. More easily readable version
`const` correctness
Formatting
ClangFormat: auto-formats C++ code
Git hook running clang-format. Don't use this!
Static analysis tools
Clang-Tidy: C/C++ linting tool, à la Google's cpplint.
clang-tidy -checks='*' *.cpp -- -std=c++11
Clang Static Analyzer: sets CC and CXX to use alternate compiler to do analysis
# Highest severity warnings only (i.e. memory leaks, etc)
cppcheck --enable=all *.cpp
# Everything except style-related linting
cppcheck --enable=warning,performance,portability,information,missingInclude \
--std=c++11 --library=std.cfg --verbose --quiet \
*.cpp
Overview articles:
Thread safety analysis (TSA)
Documentation:
Thread Safety Analysis in C and C++: CMU blog article on TSA
Thread safety analysis support for libc++, annotating mutex, lock_guard, etc
Example projects:
ClangBuiltLinux/thread-safety-analysis: Linux kernel w/ TSA annotations
Chromium, annotation macros, from base/thread_annotations.h. Macros are named different, and they use deprecated TSA names.
Lifetime safety analysis
mgehre/llvm-project: clang implementation
- note: gsl = Guidelines Support Library
Stuff I never remember
Namespacing enums in pre-C++11: https://stackoverflow.com/questions/482745/namespaces-for-enum-types-best-practices#484304
Unnecessary copies w/ `auto`
Beware unncessary copies w/ auto; default semantics is to make a copy.
// Typically there's no reason to copy.
for (const auto &Val : Container) { observe(Val); }
for (auto &Val : Container) { Val.change(); }
// Remove the reference if you really want a new copy.
for (auto Val : Container) { Val.change(); saveSomewhere(Val); }
// Copy pointers, but make it clear that they're pointers.
for (const auto *Ptr : Container) { observe(*Ptr); }
for (auto *Ptr : Container) { Ptr->change(); }
Don't evaluate `end()` in loops
// BAD
BasicBlock *BB = ...
for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
// ... use I ...
// GOOD (unless you are mutating BB; use the above loop if you are, and document you are doing so)
BasicBlock *BB = ...
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
// ... use I ...
Use `const` iterators if not mutating a structure
Expanding on the above:
BasicBlock *BB = ...
for (BasicBlock::const_iterator I = BB->cbegin(), E = BB->cend(); I != E; ++I)
// ... use I ...
Dynamic loading of DLL/DSOs
Example of loading OpenCL dynamically. Uses a Python parser script to create special header file with defines that map both functions and does appropriate type conversion. https://github.com/opencv/opencv/pull/1542/files
Boost.DLL: Boost library for easily loading DLLs (e.g. for plugins). Cross platform, but requires runtime libraryes boost_system (integrated into C++11?) and boost_filesystem (integrated into C++17).
https://github.com/knusbaum/CPP-Dynamic-Class-Loading: Class for dynamically loading classes (Linux only)