Trip Report: using std::cpp 2015 at Spain

On November 18th 2015, for the third year in a row, we had a C++ day in Spain. Our local event using::std cpp 2015 starts to consolidate as an annual event for the Spanish C++ community. Each Fall more than 100 developers join some academics and students to exchange experiences about using C++.

My full English language trip report can be found here:

Trip report: using std::cpp 2015

DISCLAIMER: Please, note that all talks where given in Spanish. We will be publishing the slides and videos in the next days. However, we feel that this activity is of interest for a wider audience...

Dive into C++14 - [3] - Generic "unique resource" wrapper

Vittorio Romeo expands the Dive into C++14 series with a new video covering the implementation of a generic "unique resource" wrapper.

Dive into C++14 - [3]

About the tutorial:

This new tutorial covers the implementation of a generic "unique resource" class, with the same ownership semantics as std::unique_ptr.

std::unique_ptr is not limited to pointer-like handles - by adapting other kinds of handles to the `NullablePointer` concept, it can still be used. Nevertheless, for educational purposes and to understand the commonalities between resource types and handle types, we're going to implement our own std::unique_ptr-like generic "unique wrapper".

The video shows the implementation of the resource class and the abstraction of the behavior of several example resource types (heap-allocated pointers, OpenGL VBOs, int-based-handle APIs).

All the code is available on GitHub. The code segments are thoroughly commented and can be read as a tutorial without watching the video, if preferred.

C++ Static Analysis using Clang -- Ehsan Akhgari

How to write/use C++ static analyzer using Clang.

C++ Static Analysis using Clang

by Ehsan Akhgari

From the article:

Large code bases typically develop rules around how various code constructs should be used.  These rules help eliminate bugs resulting from common mistakes.  C++ gives programmers a good amount of power over enforcing such rules using the facilities that the language provides.  As a simple example, if you have a class that should not be inherited from, you can mark the class as final.

Typically one finds themselves in a situation where the language doesn’t provide an easy way to enforcey something.  In that case, the solution is typically enforcing the rules through documentation, code review, etc.  Besides the obvious downside of people making mistakes when checking for these rules, detecting violations of the some rules may be completely impractical.

Clang with Microsoft CodeGen in VS 2015 Update 1--Dave Bartolomeo

You can now easily verify if Clang can compile your code on Windows:

Clang with Microsoft CodeGen in VS 2015 Update 1

by Dave Bartolomeo and the Clang/C2 feature crew

From the article:

One of the challenges with developing and maintaining cross-platform C++ code is dealing with different C++ compilers for different platforms. You write some code that builds fine with the Visual C++ compiler for your Windows-targeting build, but then your Android-targeting build breaks because Clang is stricter about standards conformance and your code was accidentally depending on Visual C++ being more permissive. Or, you write new code that builds successfully with Clang for iOS, only to find out that you used a relatively new C++ language feature that Visual C++ does not yet support, and now you have to either re-implement the code without using that language feature, or maintain a separate implementation that works with Visual C++ for your Windows build.

To make it easier to develop cross-platform code that works well for both Windows and other platforms, we’ve released an additional compiler toolset for Visual Studio called Clang with Microsoft CodeGen. This compiler uses the open-source Clang parser for C and C++, along with the code generator and optimizer from the Visual C++ compiler. This lets you compile your cross-platform code for Windows using the same Clang parser that you use for other targets, while still taking advantage of the advanced optimizations from the Visual C++ optimizer when you build for Windows. Because the new toolset uses the same Clang parser used for non-Windows targets, you won't need to have annoying #ifdefs throughout the code just to account for differences between the compilers. Also, your code can take advantage of language features that are not currently available in the Visual C++ compiler, including C99 complex types and C++14 extended constexpr support. And because the Clang-based compiler generates the same debug information format as the Visual C++ compiler, you'll still be able to debug your code with the same great Visual Studio debugger experience.

C++ Core Guidelines Checkers available for VS 2015 Update 1--Andrew Pardoe and Neil MacIntosh

Coding well in C++ is becoming easier:

C++ Core Guidelines Checkers available for VS 2015 Update 1

by Andrew Pardoe and Neil MacIntosh

From the article:

Back in September at CppCon 2015 Neil announced that we would be shipping new code analysis tools for C++ that would enforce some of the rules in the C++ Core Guidelines. (A video of the talk is available here: https://www.youtube.com/watch?v=rKlHvAw1z50 and slides are available on the ISOCpp GitHub repo.)

Quick Q: C++ regex: Conditional replace

Quick A: Use regex_token_iterator

Recently on SO:

C++ regex: Conditional replace

Use regex_token_iterator


#include <regex>
#include <string>
#include <sstream>
#include <set>
#include <map>

std::string replacer(std::string text) {
    std::string output_text;
    std::set<std::string> keywords = { "foo", "bar" };
    std::map<std::string, int> ids = {};

    int counter = 0;
    auto callback = [&](std::string const& m){
        std::istringstream iss(m);
        std::string n;
        if (iss >> n)
        {
            if (keywords.find(m) != keywords.end()) {
                output_text += m + " ";
            }
            else {
                if (ids.find(m) != ids.end()) {
                    output_text += "ID" + std::to_string(ids[m]) + " ";
                }
                else {
                    // not found
                    ids[m] = counter;
                    output_text += "ID" + std::to_string(counter++) + " ";
                }
            }
        }
        else
        {
            output_text += m;
        }
    };

    std::regex re("\\b\\w*\\b");
    std::sregex_token_iterator
        begin(text.begin(), text.end(), re, { -1, 0 }),
        end;
    std::for_each(begin, end, callback);
    return output_text;
}

 

CppCast Episode 36: rr with Robert O'Callahan

Episode 36 of CppCast the only podcast for C++ developers by C++ developers. In this episode Rob and Jason are joined by Robert O'Callahan from Mozilla to discuss the rr project.

CppCast Episode 36: rr with Robert O'Callahan

by Rob Irving and Jason Turner

About the interviewee:

Robert O'Callahan has a PhD in computer science at Carnegie Mellon and did academic research for a while at IBM Research, working on dynamic program analysis tools. At the same time he was contributing to Mozilla as a volunteer, until he switched gears to work full-time with Mozilla; Robert has been working on what became Firefox for over 15 years, mostly on layout and rendering in the browser engine and on related Web standards like CSS and DOM APIs. Lately he's been devoting about half of his time to rr.