So this function is “smarter” than just plain std::find as it will take advantage of the intrinsic find if available. So by utilizing conditional overloading and trailing decltype, we can solve this problem quite simply without having to resort to template metaprogramming. Basically, if the first function can’t be called it will try to call the next, until it can find a function that can be called. Here we use the fit::conditional adaptor to accomplish this. Let’s look at what more we can do using the Fit library without touching template metaprogramming.

Pretty print

Say we would like to write a print function that can print not only using cout but can also print the values in ranges. We could write something like this:

So the -> decltype(std::cout << x, void()) will only make the function callable if std::cout << x is callable. Then the void() is used return void from the function. We can constrain the second overload as well, but we will need some helper function in order to call std::begin and std::end using adl lookup:

Print tuples

We could extend this to printing tuples as well, but we need a for_each_tuple to call the function for each element. Fortunately, we don’t need to resort to variadic templates. We can use fit::each_arg to call each element and then use fit::fuse to convert tuple elements to function arguments. We also use fit::capture to capture f for the first argument of fit::each_arg.

Since we can’t use a lambda inside of decltype we just put fit::identity instead.

Recursive print

Even though we are using lambdas, we can easily make this recursive using fit::fix. This implements a fix point combinator, which passes the function(ie itself) in as the first argument, so we could write this:

Variadic print

So, in python, the print function can be passed multiple parameters. Once again, no metaprogramming needed for this. We can use fit::each_arg to call the print function on each argument passed in. We will rename the single argument print to simple_print, and then use that function for print: