This month’s C++ London meetup features two talks – the first on C++/Python integration and the second on C++ iterators.
Foreign Function Interface Generator (FFIG) – Jonathan Coe
Jonathan presented his hobby project, FFIG, largely written on the train during his commute (!).
I want to be able to write C++ code and call it from Python without doing any extra work
He explained that existing solutions (Boost::python and SWIG) generate interfaces that bind you to a specific binary Python implementation. Whilst a work in progress, FFIG’s generated libraries are Python library independent (meaning you can compile once and call from multiple Python libraries).
FFIG generates a C-API on top of your C++ code, which eliminates any classes/structs, but provides a library readily callable from Python. It also generates a Python (or Ruby) library that layers the objects over the C-API, to restore the original interface. A key lesson learned during development was to hide any ownership concerns from the Python layer.
Why Iterators Got It Wrong – Arno Schödl
Arno has developed a range-like library within his firm (Think-Cell), which addresses some features of iterators perceived as ugly:
// Iterators returned by begin() and end() are asymmetrical auto it = collection.begin(); // start of a non-empty collection std::cout << *it; // ok - prints the first element of the collection auto it2 = collection.end(); // end of a non-empty collection std::cout << *it2;// error - undefined behaviour, end() returns 1 past the last element! // Algorithms may return a valid element or a sentinel "border" indicator std::vector<int> v1{0, 1, 2, 3, 4}, v2{}; auto result1 = std::find(std::begin(v1), std::end(v1), 2); std::cout << *result1; // ok auto result2 = std::find(std::begin(v2), std::end(v2), 2); std::cout << *result2; // error - de-referencing end again
The summary was that his library distinguished between elements and borders instead of using iterators. Once rolled out through the codebase, this made the code clearer and avoided any chance of undefined behaviour. However, I think the availability of Eric Niebler’s Range library (or simple higher-order functions) make the STL so easy to use that these concerns shouldn’t put off new developers.