Differences between revisions 1 and 24 (spanning 23 versions)
Revision 1 as of 2016-05-12 10:02:33
Size: 106
Editor: SamatJain
Comment:
Revision 24 as of 2017-06-23 01:34:12
Size: 5001
Editor: SamatJain
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[http://clang.llvm.org/extra/clang-tidy/index.html|Clang-Tidy — Extra Clang Tools 3.9 documentation]] <<TableOfContents>>

See also:

 * [[CheatSheet/Gcc]]
 * [[VisualStudio]]

== C++ style guides ==

 * [[https://google.github.io/styleguide/cppguide.html|Google]]
 * [[http://llvm.org/docs/CodingStandards.html|LLVM]]
 * [[http://www.chromium.org/developers/coding-style|Chrome/Chromium]]
 * [[https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style|Mozilla]]
 * [[http://www.webkit.org/coding/coding-style.html|WebKit]]
 * Qt's [[https://wiki.qt.io/Qt_Coding_Style|Coding Style]] and [[https://wiki.qt.io/Coding_Conventions|Coding Conventions]]
 * [[https://github.com/isocpp/CppCoreGuidelines|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. [[http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines|More easily readable version]]

== `const` correctness ==

 * [[http://www.gotw.ca/gotw/006.htm|GotW #6: Const-Correctness]]
 * http://www.parashift.com/c++-faq-lite/const-correctness.html

== Formatting ==

[[http://clang.llvm.org/docs/ClangFormat.html|ClangFormat]]: auto-formats C++ code

 * [[https://github.com/andrewseidl/githook-clang-format|Git hook running clang-format]]. Don't use this!

== Static analysis tools ==

 * [[http://clang.llvm.org/extra/clang-tidy/index.html|Clang-Tidy]]: C/C++ linting tool, à la Google's cpplint.

{{{
clang-tidy -checks='*' *.cpp -- -std=c++11
}}}
 * [[http://clang-analyzer.llvm.org/|Clang Static Analyzer]]: sets CC and CXX to use alternate compiler to do analysis
 * [[http://cppcheck.sourceforge.net/|Cppcheck]]; [[https://github.com/danmar/cppcheck/|github:danmar/cppcheck]]

{{{#!highlight sh numbers=off
# 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:

 * [[https://arcanis.me/en/2015/10/17/cppcheck-and-clang-format/|Add cppcheck and clang-format for a cmake project]]

== Stuff I never remember ==

=== Unnecessary copies w/ `auto` ===

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

=== Don't evaluate `end()` in loops ===

{{{#!highlight c++ numbers=off

// 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:

{{{#!highlight c++ numbers=off
BasicBlock *BB = ...
for (BasicBlock::const_iterator I = BB->cbegin(), E = BB->cend(); I != E; ++I)
  // ... use I ...
}}}

== C++ move semantics/perfect forwarding ==

 * [[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]]

== C++ and Python ==

[[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!]]

== C stuff ==

Not C++, but this page is as good a place to put this as any.

 * [[http://blog.robertelder.org/7-weird-old-things-about-the-c-preprocessor/|7 Scandalous Weird Old Things About The C Preprocessor]]

== 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.

== 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]]

See also:

C++ style guides

`const` correctness

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

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.

Stuff

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