Posts

Showing posts with the label iteration

C++ : Copy Constructor versus Ignorance

Image
I've spent a bit of time reviewing someone else's code recently and I've come to an impasse with them, so they have a lot of code which will take some standard container, and the code doesn't just initialise the local copy from the passed in reference... No it'll iterate over the list of elements adding them to the class version. I have picked fault with this, it's not RAII, it looks ugly and if you're threading you can create your class instance and the member is empty or in a partially filled state before the loop within the constructor is finished... I highlight this in red below... My solution?  Just initialise the member from the reference - see the green highlight in the code below. My results from the timing below? These times are "microseconds" so tiny... But with just constructing from the existing reference we always get a lower time, quicker code... Running this test 30,000 times, trying it in different orders and with maps of upto 1000 ...