News

Metaprogramming with Modern C++: The Haskell Metaphor -- Manu Sánchez

If you are one of who have been following our post series about template metaprogramming with modern C++, at this time you should have become a C++ template Guru. At least thats what I expect wink.

You know about class templates, function templates, value parameters, type parameters, variadic templates… Your template metaprogramming toolbox is full of great things to play with. Thats good, but you want to start playing with your compiler, writting some cool metaprograms.

Metaprogramming with Modern C++: The Haskell Metaphor

by Manu Sánchez

From the article:

Template metaprogramming was used to improve perfomance on high-computing libraries too, with some clever code transformations done thanks to tmp. The best example of this is the blitz++ library, one of the first examples of a real use case of template metaprogramming.

Since C++11 the language has evolved to support some ways of metaprogramming as a common and useful thing. Metaprogramming became a first class citizen in C++, instead of the obscure, magical, and freaking way to abuse the compiler it was at the beginning...

A software design principle: Don’t make me use your design -- Diego Rodríguez-Losada

Developing code in C++ for robotics, I often faced the problem of communicating via serial port with a robot, a sensor or any other device. C and C++ are languages supposedly very close to hardware. Furthermore, they are the most common and oldest mainstream programming languages out there. So communicating over a serial port in a portable way should be straightforward.

A software design principle: Don’t make me use your design

by Diego Rodríguez-Losada

From the article:

I have seen the pattern described in the article a few times in C and C++ projects, but very rarely in other languages (at least those that I have used more as java and python) and I believe there is a reason, not directly related to software design for it: the lack of a widely used dependency manager. And no, OS package managers, installers and so, are not enough to solve this problem. Even if the authors of these libraries decide to decouple the functionality of the SerialPort basic wrapper in their design, there is little gain in it, users should still manually extract those files and integrate them in their projects, which doesn’t sound as reasonable engineering and will produce, for sure, maintenance problems and lack of updates. It is really unlikely that the authors will decide to create a separate project/library for the SerialPort, it is more effort not only to do it in the short term, but also to maintain and work with it in the mid and long terms. So developers just roll out their designs, and fill the functionality in it as required. I’ve done it so many times too...

“Effective Modern C++” from Scott Meyers Review -- CoderGears Team

Today on CoderGrears:

“Effective Modern C++” from Scott Meyers Review

by CoderGears Team

From the article:

This month a new book from Scott Meyers become available, it’s the last one of its Effective software development series (More Effective C++, Effective STL   and Effective C++ ) .

In this book Scott Meyers focus on the new C++11 and C++14 standrads, currently few books talk about them. In the other side many resources are available on the web discussing these new standards. Why I need to read this book?

Here’s our feedback...

The Python API and C++ -- Scott Prager

Some interspecies dating, appropriately on the "multi-paradigm" blog:

The Python API and C++

by Scott Prager

From the article:

Recently, for a job interview task, I was asked to write a Python module with a C or C++ implementation to solve an otherwise simple task. Obviously, I chose C++. While I had never used the Python API before, I found that the existing information on extending Python with C quite sufficient. What surprised me, however, is how little information existed for using C++. A few libraries exist, like Boost.Python, PyCXX, and some utilities that parse C++ to create Python bindings, but I didn't find much in the way of actual information without examining the sources of these libraries. ...

... I have started working on a little utility library (https://github.com/splinterofchaos/py-cxx) for personal use... [The Python C API has] many restrictions and it certainly puts a cramp on C++'s style, but the moral of this store is that just because you need to work with a C API doesn't mean you can't use modern C++ techniques.

Free CppCat for Students

CppCat is a static code analyzer integrating into the Visual Studio 2010-2013 environment. The analyzer is designed for regular use and allows detecting a large number of various errors and typos in programs written in C and C++. For the purpose of popularizing it, we've decided to launch a student-support program granting free licenses to every higher school student who will contact and ask us about that. You just need to send us a photo of your student card or transcript.

A few words about static code analysis

Static code analysis tools draw the programmer's attention to those fragments which are very likely to contain errors. Here's a simple example:

double var_z;

....

var_z = ( var_z - 16 / 116 ) / 7.787;

This code is correct from the viewpoint of the language and compiler. It's just a regular situation when division of the integer number 16 by integer number 116 results in 0. Expressions like that are necessary sometimes. However, the analyzer takes a wider perspective of the code and detects an error pattern: if the result of such an integer division is then used together with the double type, it may signal something is wrong.

The analyzer will draw your attention to this suspicious division with the following warning: V636 The '16 / 116' expression was implicitly casted from 'int' type to 'double' type. Consider utilizing an explicit type cast to avoid the loss of a fractional part. An example: double A = (double)(X) / Y;. color.c 125

The code was most likely meant to look as follows:

var_z = ( var_z - 16.0 / 116.0 ) / 7.787;

The division now results in 0,137931 instead of 0.

Static analysis tools can be treated as an extension to compiler warnings. Unlike the compiler, analyzers deal with higher-level constructs and rely on empirical methods trying to guess if the code works the way the programmer wanted it to.

A few words about the CppCat analyzer

CppCat is a static code analyzer which is easy to set and use. It is an excellent tool to get started with the static analysis methodology. It is not as powerful as its big brother PVS-Studio (see their comparison), but it is more than sufficient for most tasks. In any case, CppCat's functionality is surely quite enough for students and single developers.

The analyzer can integrate into Visual Studio 2010, 2012, 2013. Unfortunately, it can't integrate into Express editions and we can't help it as Visual Studio Express editions don't support plugins.

The analyzer supports the following languages: C, C++, C++/CLI, C++/CX.

An important feature of the analyzer is an automated analysis of freshly modified code. After compilation of modified files is finished, CppCat starts analyzing the code in background and displays a warning whenever anything suspicious is found. This feature allows programmers to detect errors at the very early development stage and thus save time on bug search and fixes.

You can download CppCat from the product's website: http://www.cppcat.com

How to get a CppCat license

Using CppCat will help you in acquiring skills of working with static analysis tools and improving your qualification. Static analyzers are growing more and more popular nowadays, so it'll be just good if you can specify in your résumé that you know how to use these tools.

To get a license you just need to send to our email [email protected] a photo or scan of your student card, transcript or other document confirming that you are a higher school student. Please specify your first and second name and the name of your university as it might be very difficult to make out this information from the photo. These data will be used to generate your personal registration key.

The license will be valid through 1 year. If you wish to be able to use CppCat after that, you will have to send us a new photo of your student card.

Building Portable Games in C++ -- Guy Kogus

Fresh on Dr. Dobb's:

Building Portable Games in C++

By Guy Kogus

The cocos-2d-x open source framework can be used to build games, apps, and other interactive software in C++. Here's a hands-on guide to using it to write and port games.

From the article:

Our game, Ready Steady Bang, was written using cocos2d-iphone, which we were considering using for our next game, Ready Steady Play. Then I remembered the C++ gaming framework cocos2d-x and its promise of cross-platform compilation.

In this article, I break down how I developed the Ready Steady Play game using cocos2d-x and discuss the porting travails of moving the final code from iOS to Android and Windows Phone 8. I used v2.2.x of cocos-2dx, but most of this text should be applicable for v3.x...

Container Algorithms -- Eric Niebler

eric-niebler-toronto.PNGSome more details about the Ranges proposal that was well received by the ISO C++ committee at this month's meeting in Urbana-Champaign:

Container Algorithms

by Eric Niebler

From the article:

... So to sum up, in the world of N4128, we have this:

  • Eager algorithms that can mutate but that don’t compose.
  • Lazy algorithms that can’t mutate but do compose.

Whoops! Something is missing. If I want to read a bunch of ints, sort them, and make them unique, here’s what that would look like in N4128:

extern std::vector<int> read_ints();
std::vector<int> ints = read_ints();
std::sort(ints);
auto i = std::unique(ints);
ints.erase(i, ints.end());

Blech! A few people noticed this shortcoming of my proposal. A week before the meeting, I was seriously worried that this issue would derail the whole effort. I needed a solution, and quick.

Container Algorithms

The solution I presented in Urbana is container algorithms. These are composable algorithms that operate eagerly on container-like things, mutating them in-place, then forwarding them on for further processing. For instance, the read+sort+unique example looks like this with container algorithms:

std::vector<int> ints =
    read_ints() | cont::sort | cont::unique;

Much nicer. Since the container algorithm executes eagerly, it can take a vector and return a vector. The range views can’t do that...

Call for volunteers: isocpp.org blog editors

We are looking for additional volunteers to be isocpp.org blog editors.

Here's what you'd do: Just read your daily RSS and other web feeds, which you're probably doing anyway -- we'll suggest a few additional feeds as appropriate ones to watch for your blog section. Then, whenever you encounter a high-quality item that you think would be of broad interest to other modern C++ developers, share it on the isocpp.org blog by posting a brief "link-to style" blog item that lets our readers know about it. Each link-item would consist of just a one-line introductory blurb, a link to the post, and an interesting representative "from the article" passage to give the reader an idea of what it's about so they can decide whether to click and read further. See the isocpp.org style guide for an example of the content and editing.

Here are the roles we are seeking to fill, with a recent example of each type of post:

  • Articles & Books editors to link to new articles, such as experience reports about using modern C++ in a project, insights about using a particular C++ feature, and related things like announcements of new high-quality modern C++ books. Example: linking to a good experience report about using modern C++.
  • "Quick Q" editors to link to useful new Q&A on sites like StackOverflow or Reddit, to make readers aware of interesting questions that have correct answers. Example: linking to a good StackOverflow question.
  • Product News editors to link to announcements about compilers, libraries, and OSS projects of interest. We'd especially like to see more posts about OSS libraries that people maybe don't know about, but should. Example: linking to a good product blog post.
  • Video & Events editors to link to new high-quality videos on C++ topics, and to announcements relating to upcoming C++ events. Examples: see the blog's video category and events category for examples.

Requirements: You should be reasonably familiar with modern C++ so that you can judge whether a prospective item is reasonably accurate, modern, and of likely broad interest to C++ developers. Note that we are actively looking for items at all levels -- introductory (such as how to use a basic C++ features), intermediate, advanced, and "experimental." We want the blog to show a balanced mix of these -- our site should reflect the full range of our community.

Workload: The expected volume will usually be about 2-5 links per week, which should take less than 15 minutes per week of your time. Once you've found something to link to during your usual reading, it only takes a couple of minutes to write the blog post to tell others about it.

Honorarium: A modest honorarium is available for students who are accepted as blog editors. If you are a student and interested in the honorarium, please mention this in your inquiry email.

If you are interested in becoming an isocpp.org blog editor, please inquire at [email protected].

Updates to my trip report

I wanted to add a few more things to my meeting trip report. I updated the trip report in-place, but for those who want to see the "diffs" I'll also post just the new parts here as a standalone post:

There were 106 experts at this meeting, officially representing 7 nations. This meeting saw a recent-record number of papers, including over 120 in the pre-meeting mailing.

In addition to the other things I mentioned, we also approved several other interesting changes, including the following highlights:

  • Adopted N4230, which allows nested namespace definitions like namespace A::B::C { } as a convenient shorthand for namespace A { namespace B { namespace C { } } }.
  • Adopted N3922, which fixes the most common pitfall with auto and {}, so that auto x{y}; now deduces the sane and expected thing, namely the type of y. Before this, it deduced initializer_list which was unfortunate and surprising. So, fixed.
  • Adopted N4086, which removes trigraphs. Yes, we removed something from C++... and something that was inherited from C! But wait, there's more...
  • Adopted N4190, and actually removed (not just deprecated) several archaic things from the C++ standard library, including auto_ptr, bind1st/bind2nd, ptr_fun/mem_fun/mem_fun_ref, random_shuffle, and a few more. Those are now all removed from the draft C++ 17 standard library and will not be part of future portable C++.

We also did work and made progress on a lot of other proposals, including modules. See the pre-meeting mailing for details about papers considered at this meeting.

 

Can Qt's moc be replaced by C++ reflection? -- Olivier Goffart

In case you missed it:

Can Qt's moc be replaced by C++ reflection?

by Olivier Goffart

From the article:

The Qt toolkit has often been criticized for extending C++ and requiring a non-standard code generator (moc) to provide introspection.
Now, the C++ standardization committee is looking at how to extend C++ with introspection and reflection. As the current maintainer of Qt's moc I thought I could write a bit about the need of Qt, and even experiment a bit.

In this blog post, I will comment on the current proposal draft, and try to analyze what one would need to be able to get rid of moc.