How can we define a function that takes any number of arguments of the same type? This is what we tackled in our previous post: How to Define A Variadic Number of Arguments of the Same Type – Part 1. C++98 templates allow a function to accept any type, C++11 variadic templates allow it to accept […]
Since C++98, templates have allowed functions to accept parameters of any type: template<typename T> void myFunction(T const& x) // T can be of any type { // … } In C++11, variadic templates have allowed functions to accept any number of parameters of any type: template<typename… Ts> void myFunction(Ts const&… xs) // the Ts can […]
How to store a variable number of objects known at compile time? This is a question that Fluent C++ reader John Koniarik sent in to me by email. Here was his exact problem, reproduced with his permission: I would like to efficiently store n-dimensional simplexes. I have defined Simplex< unsigned n > as key data […]
At the beginning of the summer, we talked on Fluent C++ about 7 projects to get better at C++ during the summer. Reader Sergio Adán has taken up the challenge, and picked up Project #1 about how to avoid duplicates in a variadic CRTP. Today as summer is drawing to an end, Sergio shares with […]
In the previous post we’ve seen how to introduce variadic templates into the CRTP pattern, and how it allowed to create classes with various sets of opt-in features. For instance, the class X would have a basic interface but also augment them by inheriting from a set of CRTP base classes: template<template<typename> typename… Skills> class X […]
Last week we’ve seen the technique of the variadic CRTP, that allowed to plug in generic extra features to a class. For instance, we’ve seen the following class X: template<template<typename> typename… Skills> class X : public Skills<X<Skills…>>… { public: void basicMethod(); }; X can accept extra features that plug into its template parameters: using X12 = […]
The CRTP is a technique that allows to add extra features to a class. Variadic templates, brought by C++11, make this technique more powerful by adding a new aspect to it: composition. Combining the CRTP with variadic templates generates customizable classes, by opting in for a various set of features, and with a expressive syntax. […]
After reading the series of posts on the CRTP, Fluent C++ reader Miguel Raggi reached out to me with the following email (reproduced with his permission): Dear Jonathan Boccara, […] After reading the posts on the curiously recurring template pattern, I’m wondering how to (expressively) implement this with 3 or more classes. Say, you have 3 […]
Before we start again, have you sent in yet your most beautiful piece of code that prints 42? Towel day is coming up, so join in the celebration!! Now that is said again, we can start 🙂 As we’ve seen in How to Make SFINAE Pretty – Part 1: What SFINAE Brings to Code, SFINAE in […]
Before we start, have you sent in yet your most beautiful piece of code that prints 42? Towel day is coming up, so join in the celebration!! Now that is said, we can start 🙂 I’m going to assume that you’re a busy developer, living and working in a busy city. And let’s say that […]