intermediate

CppCon 2015 Lambdas from First Principles: A Whirlwind Tour of C++--Arthur O'Dwyer

Have you registered for CppCon 2016 in September? Don’t delay – Early Bird 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:

Lambdas from First Principles: A Whirlwind Tour of C++

by Arthur O'Dwyer

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

Lambdas (even those mysterious generic lambdas) are just syntactic sugar atop constructs that are perfectly understandable when approached from the right direction.

We'll start with the implementation of C-style functions, then move to overloading, function templates, non-static member functions, C++11 lambdas, and then demystify C++14 generic ("auto") lambdas. Finally, we'll detour into the implementations of std::function and std::bind to show how they're different from lambdas.

Quick Q: Is final used for optimization in C++?

Quick A: Possibly.

Recently on SO:

Is final used for optimization in C++?

It can be.

An optimisation along these lines would relate to the "de-virtualization" of the virtual calls. This is not always immediately affected by the final of the class nor method. Albeit they offer help to determine this, the normal rules of the virtual functions and class hierarchy apply.

If the compiler can determine that at runtime a particular method will always be called (e.g. given the OP example, with an automatic object), it could apply such an optimisation anyway, irrespective of whether the method is final or not.

Optimisations fall under the as-if rule, that allow the compiler to apply any transformation so long as the observable behaviour is as-if the original code had been executed.

CppCon 2015 C++ Multi-dimensional Arrays...--Pramod Gupta

Have you registered for CppCon 2016 in September? Don’t delay – Early Bird 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:

C++ Multi-dimensional Arrays...

by Pramod Gupta

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

The language feature of passing a multi-dimensional array to a function without specifying all its dimensions at compile time is crucial for computational physics and applied mathematics. For example a matrix is a two dimensional array and a matrix inversion function which needs to know the size of the matrix at compile time would be of limited use. Major general purpose languages such as C, Java and C# support this feature. Of course, scientific programming languages like Fortran, Matlab and R also support this feature.

C++ is perhaps the only major programming language which does not allow passing a multi-dimensional array to a function unless the size of all the dimensions except the first one is known at compile time. Due to this limitation of C++, various libraries have been developed for using multi-dimensional arrays in C++. Some of these libraries are Blitz++, Armadillo, Eigen and boost.multi_array. These libraries are very large and complex. While they do provide a wide variety of features, they have a learning curve which may be difficult to justify for something as basic as passing multi-dimensional arrays to functions. Also the computational physics or applied mathematics code becomes dependent on a large non-standard library. Hence its usage will be limited to only those scientists who are willing to install these non-standard libraries.

The reference feature of C++ allows us to develop a multi-dimensional array class. The class has a small number of lines of code and hence the code can be included with the scientific application code. We use this class to write programs for various areas of computational physics and show that the class is easy to use and it leads to readable programs.

Quick Q: Being smart with smart pointers: avoiding shared_ptr overuse

Quick A: Use unique_ptr when you can.

Recently on SO:

Being smart with smart pointers: avoiding shared_ptr overuse

To allow unique_ptr/shared_ptr, you may use template:

// Dispatcher for make_unique/make_shared
template <template <typename...> class Ptr, typename T>
struct make_helper;

template <typename T>
struct make_helper<std::unique_ptr, T>
{
    template <typename ...Ts>
    std::unique_ptr<T> operator() (Ts&&... args) const {
        return std::make_unique<T>(std::forward<Ts>(args)...);
    }
};

template <typename T>
struct make_helper<std::shared_ptr, T>
{
    template <typename ...Ts>
    std::shared_ptr<T> operator() (Ts&&... args) const {
        return std::make_shared<T>(std::forward<Ts>(args)...);
    }
};

template <template <typename...> class Ptr, typename T, typename ... Ts>
auto make(Ts&&... args)
{
    return make_helper<Ptr, T>{}(std::forward<Ts>(args)...);
}

And then

bool get_resource_parameters(Param1& param1,..., ParamN& paramN)
{
    //...
}

template <template <typename...> class Ptr>
Ptr<resource> open_resource(...)
{
   Param1 param1;
   ...
   ParamN paramN;
   if(!get_resource_parameters(param1, ..., paramN))
       return nullptr;

   return = make<Ptr, resource>(param1, ..., paramN);
}

And check for nullptr instead of split bool and smart_pointer.

RAII versus Exceptions--Arne Mertz

There is no fight, is there?

RAII versus Exceptions

by Arne Mertz

From the article:

Recently I received a question on Twitter whether to prefer RAII over Exceptions. I have seen similar questions being asked again and again over time, so there seems to be some need for clarification...

CppCon 2015 C++ Metaprogramming: A Paradigm Shift--Louis Dionne

Have you registered for CppCon 2016 in September? Don’t delay – Early Bird 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:

C++ Metaprogramming: A Paradigm Shift

by Louis Dionne

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

Most people think metaprogramming is hard. It isn't; we just didn't have the right tools for it. This talk will present a new way of metaprogramming using the same syntax as that of normal C++. It will show how the runtime and the compile-time boundaries can be crossed almost seamlessly. It will show how compilation times can be reduced without sacrificing expressiveness. It will introduce Hana [1], a newly accepted Boost library using cutting edge features of the language in a creative way to solve the problem of metaprogramming for good.

Quick Q: Static constexpr int vs old-fashioned enum: when and why?

Quick A: A static constexpr cannot be used in an ODR context.

Recently on SO:

Static constexpr int vs old-fashioned enum: when and why?

There will be no noticeable difference for integral constants when used like this.

However, enum is actually better, because it is a true named constant. constexpr integral constant is an object which can be, for example, ODR-used - and that would result in linking errors.

#include <iostream>

struct T {
    static constexpr int i = 42;
    enum : int {x = 42};
};

void check(const int& z) {
    std::cout << "Check: " << z << "\n";
}

int main() {
    // check(T::i); // Uncommenting this will lead to link error
    check(T::x);
}

When check(T::i) is uncommented, the program can not be linked:

/tmp/ccZoETx7.o: In function `main': ccc.cpp:(.text+0x45): undefined reference to `T::i' collect2: error: ld returned 1 exit status

However, the true enum always works.

Top 10 dumb mistakes to avoid with C++ 11 smart pointers -- Deb Haldar

Discussion on various aspect of C++11 smart pointers uses.

Top 10 dumb mistakes to avoid with C++ 11 smart pointers

by  Deb Haldar

From the article:

I love the new C++ 11 smart pointers. In many ways, they were a godsent for many folks who hate managing their own memory. In my opinion, it made teaching C++ to newcomers much easier.

However, in the two plus years that I've been using them extensively, I've come across multiple cases where improper use of the C++ 11 smart pointers made the program inefficient or simply crash and burn. I've catalogued them below for easy reference.

CppCon 2015 Ranges for the Standard Library--Eric Niebler

Have you registered for CppCon 2016 in September? Don’t delay – Early Bird 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:

Ranges for the Standard Library

by Eric Niebler

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

Range-based interfaces are functional and composable, and lead to code that is correct by construction. With concepts and ranges coming to the STL, big changes are in store for the Standard Library and for the style of idiomatic C++. The effort to redefine the Standard Library is picking up pace. Come hear about one potential future of the STL from one of the key people driving the change.

Quick Q: Is this std::ref behaviour logical?

Quick A: Yes, std::ref can be reassigned (see example)

Recently on SO:

Is this std::ref behaviour logical?

A small modification to f2 provides the clue:

template<class T>
void f2(T arg)
{
    arg.get() = xx;
}

This now does what you expect.

This has happened because std::ref returns a std::reference_wrapper<> object. The assignment operator of which rebinds the wrapper. (see http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper/operator%3D)

It does not make an assignment to the wrapped reference.

In the f1 case, all is working as you expected because a std::reference_wrapper<T> provides a conversion operator to T&, which will bind to the implicit right hand side of ints implicit operator+.