Differences between revisions 11 and 12
Revision 11 as of 2016-05-28 20:54:22
Size: 1611
Editor: SamatJain
Comment:
Revision 12 as of 2016-05-29 04:30:18
Size: 2187
Editor: SamatJain
Comment:
Deletions are marked like this. Additions are marked like this.
Line 38: Line 38:

== Stuff I never remember ==

Beware unncessary copies w/ `auto`; default semantics is to make a copy.

{{{#!highlight c++ numbers=off
// 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(); }
}}}

C++ style guides

Formatting

ClangFormat: auto-formats C++ code

Static analysis tools

  • Clang-Tidy: C/C++ linting tool, à la Google's cpplint.

clang-tidy -checks='*' *.cpp -- -std=c++11

# 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

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(); }

SamatsWiki: CodingStyle/C++ (last edited 2020-09-02 20:49:51 by SamatJain)