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

Can a shared lock on a std::shared_timed_mutex be upgraded to an exclusive lock?

No, it can not. That functionality was proposed to the committee under the name upgrade_mutex and upgrade_lock, but the committee chose to reject that portion of the proposal. There is currently no work under way to re-prepose that functionality. Edit In response to the “where to go from here” edit in user3761401’s question, I’ve created … Read more

What is the correct way to initialize static data members in C++ (98, 11 and 14)

The rules have always been as follows: A const static data member (SDM) of integral or enumeration type can be initialised in class with a constant expression. A constexpr SDM must be initialised in class with a constant expression. C++17 no longer requires an initializer when the default constructor initialises every member. Also, constexpr SDMs … Read more

C++ (14) and manual memory management

Caveat: I stand by this answer since I think it presents a best practice which will improve ~95% of C++ code – probably even more. That said, please read the full comments for a discussion of some important caveats. Since it was my comment, here’s my presentation explaining this. In a nutshell: [Raw] pointers must. not. … Read more

C++ Zero-Initialization

The following MyTest testObj = {}; is not zero-initialization for MyTest, but is simply calling its default constructor. The cppreference page explains why (emphasis mine): As part of value-initialization sequence for non-class types and for members of value-initialized class types that have no constructors, including value initialization of elements of aggregates for which no initializers … Read more

Get types of C++ function parameters

This syntax is slightly different. First, because types are easier to work with than packs, a type that holds a pack. The using type=types; just saves me work in the code that generates a types: template<class…>struct types{using type=types;}; Here is the workhorse. It takes a signature, and produces a types<?…> bundle containing the arguments for … Read more

Is there any way to enforce that instances are only ever on the stack?

Disclaimer: ‘stack’ is not part of the c++ standard to my knowledge, there we have ASDVs (automatic storage duration variables). ABI might define stack. Note that sometimes these are passed in registers, which I believe is OK in your case. Define a CPS (continuation-passing style) factory method: class A { public: template<typename F, typename… Args> … Read more

Why is std::make_unique not implemented using list initialization?

In C++20, this will compile: std::make_unique<point>(1, 2); due to the new rule allowing initializing aggregates from a parenthesized list of values. In C++17, you can just do: std::unique_ptr<point>(new point{1, 2}); That won’t work with make_shared though. So you can also just create a factory (forwarding left as an exercise): template <typename… Args> struct braced_init { … Read more

STL way to access more elements at the same time in a loop over a container

The most STLish way I can imagine: std::partial_sum(std::begin(v), std::end(v), std::begin(v), std::multiplies<double>()); Example: #include <iostream> #include <vector> #include <iterator> #include <numeric> #include <functional> int main() { std::vector<double> v{ 1.0, 2.0, 3.0, 4.0 }; std::partial_sum(std::begin(v), std::end(v), std::begin(v), std::multiplies<double>()); std::copy(std::begin(v), std::end(v), std::ostream_iterator<double>(std::cout, ” “)); } Output: 1 2 6 24 Live demo link.