Differences between revisions 14 and 31 (spanning 17 versions)
Revision 14 as of 2016-05-29 04:49:59
Size: 2891
Editor: SamatJain
Comment:
Revision 31 as of 2018-07-15 02:56:10
Size: 4096
Editor: SamatJain
Comment: Move some stuff to ProgrammingLanguages/C++
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
<<TableOfContents>>

See also:

 * [[CheatSheet/Gcc]]
 * [[VisualStudio]]
Line 9: Line 16:
 * [[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
Line 82: Line 95:

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

[[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).

https://github.com/knusbaum/CPP-Dynamic-Class-Loading: Class for dynamically loading classes (Linux only)

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

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)

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