Skip to content

Instantly share code, notes, and snippets.

@foolip
Last active August 29, 2015 14:07
Show Gist options
  • Save foolip/74886e9b3a6bf7164c71 to your computer and use it in GitHub Desktop.
Save foolip/74886e9b3a6bf7164c71 to your computer and use it in GitHub Desktop.
Re-learning C++

Re-learning C++

Move semantics and rvalue references

Passing around std::vector by value now doesn't create temporary copies, without the return value optimization.

Scott: Understand std::move and std::forward

Herb: Use return-by-value way more often

Scott, Andrei & Herb: Overuse of std::move and references vs. values

Panel: When to use && parameters?

Panel: The magic of template <typename T> f(T&&)

Stephan: Mistakes with rvalue references

Investigate: In which cases does having a move constructor and move assignment operator help performance?

const

Bitwise/physical constness vs logical constness. The advice in EC++ item 3 is "Compilers enforce bitwise constness, but you should program using logical constness."

Scott: Make const member functions thread-safe

Andrei, Scott & Herb: Use const to mean thread-safe?

Investigate: Any effects on generated code? Common sub-expression elimination.

constexpr

???

Panel: expanding constexpr

When should one use { } to initialize?

Scott: Distinguish () and {} when creating objects

Smart pointers

std::unique_ptr and std::shared_ptr

Herb: Use smart pointers effectively ... but still use lots of raw * and &

Interactive Panel: Reference cycles with std::shared_ptr (STL quote: "Ownership is a directed acyclic graph.")

Range-based for loop

for (auto& element : elements) element->setCurrentTime(0);

Herb: Prefer range-for

auto

Herb Sutter says use it wherever possible. Good idea?

Scott: Prefer auto to explicit type declarations

Herb: Let's talk about auto

Andrei, Scott & Herb: When to use the auto keyword?

Scott's C++ Type Deduction and Why You Care: NDC 2014 & CppCon 2014

decltype

Investigate!

Exceptions

Scott: Declare functions noexcept whenever possible

Andrei, Scott & Herb: Use exceptions?

Templates

template <typename T> T add(const T& a, const T& b) { return a + b; }

RAII

Investigate: interaction with exceptions.

Threading

std::async is cool.

Scott: Make std::threads unjoinable on all paths

Andrei, Scott & Herb: std::async

Andrei, Scott & Herb: std::thread

explicit constructors

Always use it?

Multiple return values

Can do it with std::tuple and std::tie.

Forward declaration

Herb: forward declare Widget and Gadget when declaring Widget f(Gadget);

Integer types

Google C++: Use int or a precise type from stdint.h

Panel: Use int, avoid unsigned

Panel: More on unsigned

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment