details of std::make_index_sequence and std::index_sequence

I see sample code around there, but I really want a dumbed down step by step explanation of how an index_sequence is coded and the meta programming principal in question for each stage. What you ask isn’t exactly trivial to explain… Well… std::index_sequence itself is very simple: is defined as follows template<std::size_t… Ints> using index_sequence … Read more

TMP: how to generalize a Cartesian Product of Vectors?

Simpler recursive solution. It takes vectors as function arguments, not as a tuple. This version doesn’t build temporary tuples, but uses lambdas instead. Now it makes no unnecessary copies/moves and seems to get optimized successfully. #include<tuple> #include<vector> // cross_imp(f, v…) means “do `f` for each element of cartesian product of v…” template<typename F> inline void … Read more

C++11 “overloaded lambda” with variadic template and variable capture

Overload resolution works only for functions that exist in a common scope. This means that the second implementation fails to find the second overload because you don’t import function call operators from overload<Frest…> into overload<F0, Frest…>. However, a non-capturing lambda type defines a conversion operator to a function pointer with the same signature as the … Read more

void_t “can implement concepts”?

Yes, concepts lite basically dresses up SFINAE. Plus it allows deeper introspection to allow for better overloading. However that only works if the concept predicates are defined as concept bool. The improved overloading does not work with the current concept predicates, but conditional overloading can be used. Lets look how we can define predicates, constrain … Read more

Deducing first template argument with other template parameters defaulted

This is perfectly valid code, and gcc is right. The “feature” was introduced in C++17. It’s not really a feature because it is a defect report. MyDelegate matches the partial specialization of signature_traits, and so it should be taken as gcc correctly does. Note that it works because the second template parameter is defaulted. The … Read more

How can I check if a type is an instantiation of a given class template? [duplicate]

I came up with the following solution, using C++11 variadic templates and simple partial specialization: #include <type_traits> template < template <typename…> class Template, typename T > struct is_instantiation_of : std::false_type {}; template < template <typename…> class Template, typename… Args > struct is_instantiation_of< Template, Template<Args…> > : std::true_type {}; It could be adapted to C++03 by … Read more

building and accessing a list of types at compile time

A solution utilizing a common header, variadic templates and a macro: // Header common.h // A distinct Void type struct Void {}; template <typename …> struct concat; template <template <typename …> class List, typename T> struct concat<List<Void>, T> { typedef List<T> type; }; template <template <typename …> class List, typename …Types, typename T> struct concat<List<Types…>, … Read more

How to make generic computations over heterogeneous argument packs of a variadic template function?

Since I was not happy with what I found, I tried to work out a solution myself and ended up writing a small library which allows formulating generic operations on argument packs. My solution has the following features: Allows iterating over all or some elements of an argument pack, possibly specified by computing their indices … Read more

Checking a member exists, possibly in a base class, C++11 version

Actually, things got much easier in C++11 thanks to the decltype and late return bindings machinery. Now, it’s just simpler to use methods to test this: // Culled by SFINAE if reserve does not exist or is not accessible template <typename T> constexpr auto has_reserve_method(T& t) -> decltype(t.reserve(0), bool()) { return true; } // Used … Read more