2 ms·
How do other C++ programmers verify that they're getting this stuff right? I shared your frustation then recognized it's origin: me not properly understanding
by stinos 12y ago
How do other C++ programmers verify that they're getting this stuff right?
I shared your frustation then recognized it's origin: me not properly understanding the stuff because of lack of knowledge and training. So I spent a couple of hours searching the internet, writing some code and verifying behavior (1) until I knew what I needed to know about the subject. Then applied that knowlegde to some heavy template code using all new features (variadic templates/perfect forwarding/...). Figured I still didn't know enough so I learnt some more. In the end it took a while, but now I'm pretty confident with it and the resulting code benefits from it. I used to care way too much about spurious copies etc but lately I found that I hardly make classes copyable anymore, only movable, which also automatically seemed to make typical issues with sharing and lifetime fade away.
(1) sometimes looking at assembly, which is always a good way to get deep understaning, mostly by using 'Noisy' classes like
class Noisy
{
public:
Noisy() { print( "ctor" ); }
Noisy( Noisy&& ) { print( "move ctor" ); }
//etc, you get the point
};
and using it in cases like yours. And yes defining move vs copy constructor might change what gets invoked but if you have to care that much which one you should ask yourself if you need the copying.