C++ Just use lambdas

In the previous post, I have presented variadic function template as an simple but effective print() function. It is perhaps “simple” looking but it requires non trivial modern C++ knowledge to code and to understand one.

Now forget the templates. 

Use lambdas.  And by using modern C++ we do not even need to spend time carefully coding variadic templates or even templates.

We can simply use lambdas. Modern C++ also deliver generic lambdas and with variable number of arguments too.

As simple as this. No hairy template declarations. if constexpr () is a C++17 “novelty”. It happens at compile time and thus if there are no arguments given to print() it will  compile to “nothing” on that call.

Same as previous template version this function is not recursive. Parameter pack is unpacked with the great help of C++17 folding.

Also embedding it in the  namespace, in some header, makes the inline print() unique on the application level.

Enjoy …

Forget the templates. Use lambda.
Forget the templates. Use lambda.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.