Posts

Showing posts with the label std

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 ...

C++ : Pass-By-Reference Or Die

Image
Before today's Post, I'm on a mission folks, to get 1000 subs on YouTube.  If only 5% of viewers here subscribed we've have met this target in one month... I've just had group code review of one of my personal projects, and been rather surprised by the vitriol levelled at one of my practices.... Pass by Reference. The reviewer, one of a group of peers, has had major issues with the project (my personal) insistance on passing by reference wherever possible, in C++ this takes the form of an additional ampersand on parameter definitions; maybe this was the chaps problem, he has to type an ampersand? So his problem?  Well, without the actual code we'll simplify and use the Compiler Explorer (from Godbolt.org) and we'll take up their basic square function example, it starts up thus: Giving the assembler: On the right, and this chap had taken time to prepare a whole slide show of functions, usually simple, and present them at this code review, showing this kind of t...