- Examples

In this lesson, we'll look at an examples of the fold expressions.

Example 1: Fold Expression

Press + to interact
// foldExpression.cpp
#include <iostream>
template<typename... Args>
bool all(Args... args) { return (... && args); }
template<typename... Args>
bool any(Args... args) { return (... || args); }
template<typename... Args>
bool none(Args... args) { return not(... || args); }
int main(){
std::cout << std::endl;
std::cout << std::boolalpha;
std::cout << "all(true): " << all(true) << std::endl;
std::cout << "any(true): " << any(true) << std::endl;
std::cout << "none(true): " << none(true) << std::endl;
std::cout << std::endl;
std::cout << "all(true, true, true, false): " << all(true, true, true, false) << std::endl;
std::cout << "any(true, true, true, false): " << any(true, true, true, false) << std::endl;
std::cout << "none(true, true, true, false): " << none(true, true, true, false) << std::endl;
std::cout << std::endl;
std::cout << "all(false, false, false, false): " << all(false, false, false, false) << std::endl;
std::cout << "any(false, false, false, false): " << any(false, false, false, false) << std::endl;
std::cout << "none(false, false, false, false): " << none(false, false, false, false) << std::endl;
std::cout << std::endl;
}

Explanation

In the above example, we have ...

Access this course and 1400+ top-rated courses and projects.