Quick sort at compilation time using C++11 variadic templates

Have you also looked at its memory consumption? Note that quicksort itself is worse than linear, with a quite bad worse case runtime. This multiplies with the worse than linear runtime behaviour of certain steps of template compilation and instantiation (sometimes those are exponentional). You should maybe graph your compiletime for various datasets to observe … Read more

Get types of C++ function parameters

This syntax is slightly different. First, because types are easier to work with than packs, a type that holds a pack. The using type=types; just saves me work in the code that generates a types: template<class…>struct types{using type=types;}; Here is the workhorse. It takes a signature, and produces a types<?…> bundle containing the arguments for … Read more

What are the 6 dots in template parameter packs? [duplicate]

Why does libstdc++ use … … in it’s implementation of is_function? If we check out the cppreference section for std::is_function it gives a sample implementation and says for the first … … case: // specialization for variadic functions such as std::printf template<class Ret, class… Args> struct is_function<Ret(Args……)> : std::true_type {}; so we need the second … Read more

A better LOG() macro using template metaprogramming

Here is another expression template which seems to be even more efficient based on some tests that I’ve run. In particular, it avoids creating multiple functions for strings with different lengths by specializing operator<< to use a char * member in the resulting structure. It should also be easy to add other specializations of this … Read more

How do you loop over a parameter pack using a pack expansion?

One of the places where a pack expansion can occur is inside a braced-init-list. You can take advantage of this by putting the expansion inside the initializer list of a dummy array: template<typename… Args> static void foo2(Args &&… args) { int dummy[] = { 0, ( (void) bar(std::forward<Args>(args)), 0) … }; } To explain the … Read more

How can I prevent a variadic constructor from being preferred to the copy constructor?

You can use some ugly SFINAE with std::enable_if, but I’m not sure it is better than your initial solution (in fact, I’m pretty sure it’s worse!): #include <memory> #include <type_traits> // helper that was not included in C++11 template<bool B, typename T = void> using disable_if = std::enable_if<!B, T>; template<typename T> struct Foo { Foo() … Read more

C++11: I can go from multiple args to tuple, but can I go from tuple to multiple args? [duplicate]

Try something like this: // implementation details, users never invoke these directly namespace detail { template <typename F, typename Tuple, bool Done, int Total, int… N> struct call_impl { static void call(F f, Tuple && t) { call_impl<F, Tuple, Total == 1 + sizeof…(N), Total, N…, sizeof…(N)>::call(f, std::forward<Tuple>(t)); } }; template <typename F, typename Tuple, … Read more