June 2018
Intermediate to advanced
348 pages
8h 45m
English
The C++ 17 standard added support for fold expressions to ease the generation of variadic functions. The Compiler does pattern matching and generates the code by inferring the intent of the programmer. The following code snippet demonstrates the idea:
//---------------- Folds.cpp//--------------- Requires C++ 17 (-std=c++1z )//--------------- http://en.cppreference.com/w/cpp/language/fold#include <functional>#include <iostream>using namespace std;template <typename... Ts>auto AddFoldLeftUn(Ts... args) { return (... + args); }template <typename... Ts>auto AddFoldLeftBin(int n,Ts... args){ return (n + ... + args);}template <typename... Ts>auto AddFoldRightUn(Ts... args) { return (args + ...); }template <typename... Ts>auto ...