Differences between revisions 30 and 31
Revision 30 as of 2018-06-26 00:52:31
Size: 6432
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 96: Line 96:
== 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.

== C++11 ==

=== Smart pointers ===

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

== 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/hughperkins/Jinja2CppLight|hughperkins/Jinja2CppLight: (very) lightweight version of Jinja2 for C++]]
Line 140: Line 103:

== Videos ==

 * [[https://www.youtube.com/watch?v=3MB2iiCkGxg|Curiously recurring C++ bugs at Facebook]]
  * Use `-fsanitize-address` and `-fsanitize-address-use-after-scope`
  * Use `-Wshadow`, and deal with noisy warnings

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)