Meeting 13 July 2017

Toronto tweets

Exception handling in constructors

 1A::A(int& n) try : B(n), cObject(0), x(-1), y(n) {
 2}
 3catch(Exception& e) {
 4    //Option 1: Throw same exception
 5    //Option 2: Log and thrown another exception
 6    //Option 3: Exception thrown automatically on reaching end of handler
 7}
 8catch(AnotherException& e) {
 9    //Option 1: Throw same exception
10    //Option 2: Log and thrown another exception
11    //Option 3: Exception thrown automatically on reaching end of handler
12}
  • Use with constructors that have initializer list that calls a user-defined constructor that can throw.
  • You can replace the exception being thrown and cause some useful side effects such as log the failure.

The rise of the new language MC++

http://cppdepend.com/blog/?p=171

  • Folly by Facebook as a modern C++ library example
  • auto everywhere
  • nullptr
  • std::shared_ptr
  • Scoped enums
  • static_assert
  • variadic templates
  • range for loops
  • std::initializer_list
  • noexcept
  • std::thread
  • unordered containers
  • default and delete special member functions
  • override
  • lambdas
  • std::move (doesn’t move) and std::forward (doesn’t forward)

To move or not to move

http://www.stroustrup.com/move.pdf

Discusses issues with generating implicit copy and move operations.

To have move useful, it must be implicitly generated in many cases. To minimize surprises and bugs, no move operations should be generated for classes with a user-specified copy, move, or destructor. To keep the rules consistent, the generation of copy operations should be deprecated for classes with a user-specified copy, move, or destructor.

Ninja build system

Thinking Outside the Synchronisation Quadrant, by Kevlin Henney, ACCU 2017

  • synchronization only needed for shared mutable data
  • instead of threads why not use message passing between processes
  • “processes” can be lightweight (Erlang)
  • agents react to messages
  • no synchronization needed within message handlers

‘Meaningful’ casts, by Vittorio Romeo — Meeting C++ 2015

Range-checked casts

1template <typename TOut, typename TIn>
2constexpr auto to_num(const TIn& x) noexcept {...}
1int a{10};
2to_num<float>(a);
3to_num<int>(-1); // OK
4to_num<unsigned int>(-1); // Run-time assertion
5to_num<float>(NAN); // Run-time assertion

Also: from_enum, to_enum, support for std::aligned_storage_t, casts along inheritance hierarchy, etc.

C++ Rvalue references explained

By Thomas Becker

Table of Contents

  1. Introduction
  2. Move Semantics
  3. Rvalue References
  4. Forcing Move Semantics
  5. Is an Rvalue Reference an Rvalue?
  6. Move Semantics and Compiler Optimizations
  7. Perfect Forwarding: The Problem
  8. Perfect Forwarding: The Solution
  9. Rvalue References and Exceptions
  10. The Case of the Implicit Move
  11. Acknowledgments and Further Reading

An Effective C++11/14 Sampler, by Scott Meyers (Going Native, 2013)

Video (1h 15m)

  • Understand std::move and std::forward
  • Declare functions noexcept whenever possible
  • Make std::threads unjoinable on all paths
 1class ThreadRAII {
 2public:
 3    typedef void (std::thread::*RAIIAction)();
 4    ThreadRAII(std::thread&& thread, RAIIAction a)
 5    : t(std::move(thread)), action(a) {}
 6    ~ThreadRAII()
 7    {if (t.joinable()) (t.*action)();} // race condition?
 8    std::thread& get() {return t;}
 9private:
10    RAIIAction action;
11    std::thread t;
12};
13
14ThreadRAII t1(std::thread(doThisWork), &std::thread::join);
15ThreadRAII t1(std::thread(doThisWork), &std::thread::detach);

Outcome v2 is feature complete

1outcome::result<int/*, std::error_code*/>
2convert(const std::string& str) noexcept;

Boost review feedback addressed.

Undefined Behavior in 2017

by John Regehr (Professor of Computer Science, University of Utah, USA) and Pascal Cuoq

Article

  • Goal 1: Every UB (yes, all ~200 of them, we’ll give the list towards the end of this post) must either be documented as having some defined behavior, be diagnosed with a fatal compiler error, or else — as a last resort — have a sanitizer that detects that UB at runtime.
  • Goal 2: Every UB must either be documented as having some defined behavior, be diagnosed with a fatal compiler error, or else have an optional mitigation mechanism that meets the requirements above.

Presenting Code

  • Use dark-on-light colour scheme
  • Avoid dark backgrounds as syntax-highlighted code becomes invisible or very hard to read
  • Consider effects of video compression
  • Think about colour-blind developers