Thursday, February 26, 2015

Blast from the past: C++ revisited

Ok, I'll admit that C++ doesn't appear to be as popular as some other current languages and may not draw the interest of many new developers but it was one of the first handful of languages I learned - right after CoCo Basic, 6809E assembler, Microsoft QuickC 1.0 and then Turbo C++.  I used C/C++ extensively throughout college and C++ was the primary language I used for roughly half of my career.  I have a number of fond memories from those times - fortunately the memories of corrupted memory and segfaults faded.

Anyways, I decided to check and see what has changed over the last 10 years or so.  I figured that there likely have been a number of improvements and I am pleasantly surprised.  I picked up a copy of "Effective Modern C++ by Scott Meyers; published by O'Reilly" and working my way through it. This book covers newer stuff from the C++11 and C++14 standards.  In the process, I am trying out Eclipse (initially setup for Java EE and Reports developers) with CDT installed. I had Cygwin installed already with many of the GNU tools installed (including G++ 4.8.2).

The CDK tooling doesn't seem quite as refined as the Turbo C++ or old Visual Studio tools I used years ago but it is workable.  I may just need time to adapt a bit to something new.

A quick note on accessing the most current C++ features in Eclipse; you should set the language standard to C++1y which appears to map to C++14.  If you are just working from a command line and make files; pass '-std=c++1y' on the g++ command line. 



 Here is a short example using 'auto' and 'lambda' functionality.



The output from the above is:
My Name is scott
4 + 1 =5
lambda call result:120
'auto' lambda called; result=720


Here is an extended example passing lambda defined functions to a function.



Output:
This examples produces 12.28

I do want to note that the CDK tooling indicates a problem/confusion with the return type of function "fn" in this case but the g++ compiler produces a working executable. If you simply declare the return type of "fn" as double instead of auto the error indication goes away.  Hard to say what should happen here without digging into the standards doc I am thinking.

Overall, I'm happy to see the changes made to the language so far.  Other interesting changes appear to include threading/concurrency support and "move/perfect forwarding".  I'll have to work on some examples at some point.  I have always enjoyed C++ templates and want to revisit those when time permits.



No comments:

Post a Comment