Size: 5001
Comment:
|
Size: 5405
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 120: | Line 120: |
== C++11 == [[https://arne-mertz.de/2015/12/shared_ptr-versus-unique_ptr-in-factory-functions/|shared_ptr Versus unique_ptr in Factory Functions]] [[https://softwareengineering.stackexchange.com/questions/274801/raw-weak-ptr-unique-ptr-shared-ptr-etc-how-to-choose-them-wisely|c++ - raw, weak_ptr, unique_ptr, shared_ptr etc... How to choose them wisely? - Software Engineering Stack Exchange]] |
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:
Stuff I never remember
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 ...
C++ move semantics/perfect forwarding
C++ and Python
pybind/pybind11: Seamless operability between C++11 and Python
tbenthompson/cppimport: Import C++ files directly from Python!
C stuff
Not C++, but this page is as good a place to put this as any.
STL
Wrap an existing C-style pointer to an array with std::vector: http://stackoverflow.com/a/15203325/14878; modifies std::vector internals to work.
C++11
shared_ptr Versus unique_ptr in Factory Functions
Stuff
awesome-cpp: A curated list of awesome C/C++ frameworks, libraries, resources, and shiny things. Inspired by awesome-... stuff.