How to get the difference between two points in time in milliseconds

std::chrono::duration has two template parameters, the second being exactly the unit of measure. You can invoke std::chrono::duration_cast to cast from one duration type to another. Also, there is a predefined duration type for milliseconds: std::chrono::milliseconds. Composing this together: auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(foo – now); To get the actual number of milliseconds, use duration::count: auto ms … Read more

Why does std::optional not have a specialization for reference types?

When n3406 (revision #2 of the proposal) was discussed, some committee members were uncomfortable with optional references. In n3527 (revision #3), the authors decided to make optional references an auxiliary proposal, to increase the chances of getting optional values approved and put into what became C++14. While optional didn’t quite make it into C++14 for … Read more

Overload resolution between value, rvalue reference, const lvalue reference

What are the rules here? As there is only one parameter, the rule is that one of the three viable parameter initializations of that parameter must be a better match than both the other two. When two initializations are compared, either one is better than the other, or neither is better (they are indistinguishable). Without … Read more

Why is conversion from string literal to ‘char*’ valid in C but invalid in C++

Up through C++03, your first example was valid, but used a deprecated implicit conversion–a string literal should be treated as being of type char const *, since you can’t modify its contents (without causing undefined behavior). As of C++11, the implicit conversion that had been deprecated was officially removed, so code that depends on it … Read more

C++11 rvalues and move semantics with return statement

First example std::vector<int> return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return tmp; } std::vector<int> &&rval_ref = return_vector(); The first example returns a temporary which is caught by rval_ref. That temporary will have its life extended beyond the rval_ref definition and you can use it as if you had caught it by value. This is very similar to … Read more

Why does std::tuple break small-size struct calling convention optimization in C++?

It seems to be a matter of ABI. For instance, the Itanium C++ ABI reads: If the parameter type is non-trivial for the purposes of calls, the caller must allocate space for a temporary and pass that temporary by reference. And, further: A type is considered non-trivial for the purposes of calls if it has … Read more