C++ is huge and getting bigger every day. So, I keep discovering interesting new features that I like to note down for use in my own code. Below are my notes from this talk. I do not note aspects that I already know well. This talk has examples that are small but illustrative, so if you hit any of these features, you should see the video to look at the examples.
std::endwork on C arrays too. Note that this is only when the array size is known. So, the array must have been created in the same local scope.
decltypededuces the type of an expression. So its use is in type expressions. For example, as template arguments.
// vector of the return type of function f std::vector<decltype(f())> vals;
- Internally, it is used by
autoto deduce type of expression
- Lambda expression yields an unnamed function object. The tiny examples in the talk are good.
- This is to help programmers find errors. For example, when virtual method in base class is not const and in derived it is. Programmer might miss this error. If virtual method in derived class is declared
overrideand it is actually not, compiler will complain.
- This smart pointer is not copyable, but movable. It is deleted when pointer goes out of scope.
- Many functions create a heap-allocated object and return it. Traditionally, programmers had to worry about the ownership and lifetime of such a returned object. Return it as unique_ptr and forget about these worries.
- Also great for storing such heap-allocated objects in containers.
This C++11 feature was something new to me! I did not understand how to apply it either. I might need to study this in future.
error_coderepresents an error. It has
error_valueintegral value indicating what is the error. It has
categoryindicating domain of error value.
categoryis an abstract base class implementing conversion of
error_valueto human readable message.
- There are a whole bunch of old C, C++, Unix and POSIX time functions. They are not platform agnostic, have low time resolutions, have no type safety (milliseconds value can be passed to a function that takes in microseconds and so on) and are not monotonic. Monotonic in this context means that if you measure a time before DST is turned on and after it, the latter value should always be larger, though the wall clock may have been turned back by DST.
- Chrono introduces a clock with its own epoch (start of life) and its own resolution.
time_point: A point in time relative to epoch. It has its resolution encoded inside it.
time_duration: Difference of two time points. It has its resolution encoded inside it.
- Because these types have their resolutions embedded inside, two durations of different resolutions can be added together to produce a duration that has resolution that is highest or higher than both. They can be passed to function that accepts in a different resolution. The template machinery ensures that it all converts correctly.