Posts

Showing posts with the label source

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

Introduction to C++ : Starting C++ Series Part 1

A few of you maybe aware of the book on Python I wrote , and published, last year?  And I've had at least one reader get in touch for a second part.  Unfortunately my gaze has passed over Python and returned to where I live.  The world of C++. I have a particular problem with the C++ developers I'm meeting of late, they're either simply not C++ programmers, being an actual mix of good and bad C programmers or just not programmers at all (in one case).  Then even when they are very good C Programmers, there's been a mix of the up-take on ideas and feature benefits of modern C++ itself. Its to and for these fair folk I have begun to write about C++, a new book, based on my own real experience but tempered with where I believe teams and individuals are going wrong when converting their skills to modern C++. For the programmers reading here now, it starts with a chapter zero... Lets take a sneak-peek.... Chapter 0: Introducing C++ It is incredibly hard to introduce the C...

C++ : The unrandom random number...

Image
I've been working in some C++, with boost to be precise, the machine I'm working towards finally has a processor with SSE3 in it, and so I've been to revisit the GUID generation code, boost specifies a couple of defines you can set up before incluiding the uuids header to help... #include <iostream> #ifndef BOOST_UUID_USE_SSE3 #define BOOST_UUID_USE_SSE3 #endif #include <boost/uuid/uuid.hpp> #include <boost/uuid/uuid_io.hpp> #include <boost/uuid/uuid_generators.hpp> #include <boost/lexical_cast.hpp> const std::string GetGuid(); int main () { for (unsigned int i(0); i < 100000; ++i) { std::cout << GetGuid() << "\r\n"; } } const std::string GetGuid() { boost::uuids::uuid l_guid = boost::uuids::random_generator()(); return boost::lexical_cast<std::string>(l_guid); } This code looks fairly innocuous, "GetGuid" is the key part, you may argue that you're always setting the random number gener...