advanced

checking expression validity in-place with C++17--Vittorio Romeo

An introduction to the world of C++17.

checking expression validity in-place with C++17

by Vittorio Romeo

From the article:

When writing generic code, it is sometimes useful to check whether or not a particular SFINAE-friendly expression is valid (e.g. to branch at compile-time). Let's assume that we have the following class declarations...

struct Cat
{
    void meow() const { cout << "meow\n"; }
};

struct Dog
{
    void bark() const { cout << "bark\n"; }
};

...and that we would like to write a template function make_noise(x) that calls x.meow() and/or  x.bark() if they are well-formed expressions:

template <typename T>
void make_noise(const T& x)
{
    // Pseudocode:
    /*
        if(`x.meow()` is well-formed)
        {
            execute `x.meow();`
        }
        else if(`x.bark()` is well-formed)
        {
            execute `x.bark();`
        }
        else
        {
            compile-time error
        }
    */
}

In this article I'll show how to implement the pseudocode in:

C++11: using std::void_t and std::enable_if.

C++14: using boost::hana::is_valid and vrm::core::static_if.

C++17: using if constexpr(...), constexpr lambdas, and std::is_callable. This version will allow expression validity to be checked in-place (i.e. directly in the if constexpr predicate). Variadic preprocessor macros will also be used to make the user code easier to read and maintain...

Quick Q: Is there a case where ellipsis(vararg) should be preffered over variadic templates?

Quick A: There are several special cases where you may want that, but in general no.

Recently on SO:

Is there a case where ellipsis(vararg) should be preffered over variadic templates?

  1. If you provide a C API with C++ implementation, then templates are not an option for the API. Varargs are.
  2. If you need to support a compiler that doesn't support C++11 or newer standard, then variadic templates are not available. Varargs are.
  3. If you need a compilation firewall. I.e. you need to hide the implementation of the function from the header, then variadic template is not an option. Varargs are.
  4. On memory constrained systems (embedded), the different functions generated by the template may introduce too much bloat. That said, such systems are typically also real time, in which case varargs might also unacceptable due to branching and stack usage.

void foo(T& out) - How to fix output parameters--Jonathan Müller

Or how to improve readability and reduce errors.

void foo(T& out) - How to fix output parameters

by Jonathan Müller

From the article:

There are some cases where you need to return a value from a function but cannot use the return value. It happens, for example, in functions where you want to return multiple values at once. While you can pass multiple inputs to a function - the parameters, you cannot pass multiple return values in the same way.

C++ programmers tend to use a good old (lvalue) reference for that. You take a non-const reference as parameter and assign the output to that reference. The caller will pass a variable and upon function completion find the value of the variable changed.

Yet this approach has some problems: For starters, it is not obvious when just looking at the call that the variable is going to be changed. This is the reason that C++ style guides such as the one used by Google recommend using a pointer for that. The caller then has to explicitly pass in the address of the variable, making it explicit.

But with a pointer you can now pass in nullptr, you have to check for that in the function: A pointer where you really mean “reference” does not follow the guidelines I’ve been advocating for.

So is there not a universal solution?

There is, but first we need to understand the full scope of the problem.

DLib 19.2 released

Today a new version of DLib is available:

DLib 19.2 released

Release notes

by Davis King

From the article:

... So the obvious thing to do was to add an implementation of MMOD with the HOG feature extraction replaced with a convolutional neural network.  The new version of dlib, v19.2, contains just such a thing.  On this page you can see a short tutorial showing how to train a convolutional neural network using the MMOD loss function.  It uses dlib's new deep learning API to train the detector end-to-end on the very same 4 image dataset used in the HOG version of the example program.  Happily, and very much to the surprise of myself and my colleagues, it learns a working face detector from this tiny dataset.

Metashell 3.0.0 is available

Metashell provides a compile-time debugger for debugging template instantiations and macro usage.

Metashell 3.0.0 is available

From the website:

Some of the major new features in 3.0.0:

  • Profiling template instantiations
  • Debugging template instantiations in expressions involving SFINAE
  • Displaying preprocessed expressions

Commands for exploring the available headers (similar to the which and ls commands).
The full list of changes can be found here.

An online demo can be found at http://metashell.org/about/demo/

Installers can be downloaded from http://metashell.org/getting_metashell/installers#version-300

 

CppCon 2015 Boost Units Library for Correct Code--Robert Ramey

Have you registered for CppCon 2016 in September? Don’t delay – Late registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2015 for you to enjoy. Here is today’s feature:

Boost Units Library for Correct Code

by Robert Ramey

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

I will give a presentation on the Boost Units library.

This library implements a zero runtime facility for performing dimensional analysis checking and automatic units conversion on C++ expressions. I have found this indispensable for coding scientific programs involving a variety of complex physical units. The documentation of the Boost Units library is totally complete and accurate, but totally inpenetrable. I had to spend way too much time figuring out how to use this. By attending this meeting, you're going to avoid this pain and just get the benefit of simpler programs that contain fewer bugs.

CppCon 2015 Secure C++ Programming--Gwendolyn Hunt

Have you registered for CppCon 2016 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2015 for you to enjoy. Here is today’s feature:

Secure C++ Programming

by Gwendolyn Hunt

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

Security vulnerabilities are fundamentally defects in our code. We know many of these defects stem from string processing, buffer overflows and integer underflow and overflows. These defects become security vulnerabilities when an attacker can crash an application, cause undefined behavior that leads to a Denial of Service, privilege escalation or hidden installation of rogue software.

So how do we build more secure C++ software? It starts by gaining an understanding of the basics of security vulnerabilities and how to identify them using the rich set of tools we now have available. With this foundation we can build a development culture where security considerations are pervasive and treated as important as program and algorithm correctness.

This session begins with a survey of common C/C++ string, integer and STL container issues and mitigations for these vulnerabilities. Follows with two detailed examples of vulnerabilities and how to fix their problems. Finishes with a survey of tools and references we have available today.

Episode Eleven: To Kill a Move Constructor--Agustín "K-ballo" Bergé and Howard Hinnant

Following here is an advanced article about the behavior of C++ concerning copy and move operations. A more simple version is provided for a quicker and easier understanding of the best practices. The sum up is, don't declare as deleted a move constructor!

A more user friendly version

by Howard Hinnant

Episode Eleven: To Kill a Move Constructor

by Agustín "K-ballo" Bergé

From the article:

Unlike copy operations, which are provided by the compiler if not user declared, move operations can and often are suppressed such that a class might not have one. Furthermore, it is possible for a class to have a —user declared— move operation which is both defined as deleted, and at the same time ignored by overload resolution, as if it didn't exist...

 

CppCon 2015 `for_each_argument` explained and expanded--Vittorio Romeo

Have you registered for CppCon 2016 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2015 for you to enjoy. Here is today’s feature:

`for_each_argument` explained and expanded

by Vittorio Romeo

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

During January 2015, Sean Parent posted a very interesting short piece of code on Twitter. The code iteratively iterates at compile-time over any number of function arguments, forwarding them one by one to a callable object.

How does this code work? What are the possible use cases? Can we make it even more generic and useful?

My talk answers all of the questions above, using independently compiled chronologically sequential code segments that show the audience the analysis and improvement process of `for_each_argument`.