Created
October 9, 2017 03:35
-
-
Save LeftofZen/10dbdd7913768eeb5f0e1f681ac81bbe to your computer and use it in GitHub Desktop.
C++17 Fold expressions in C++11/14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <functional> | |
struct fold_base | |
{ | |
template <typename T> | |
static T fold(T arg) | |
{ | |
return arg; | |
}; | |
}; | |
template <typename UnaryOp> | |
struct fold_left_trait : public fold_base | |
{ | |
// bring in the recursive base case from the parent | |
using fold_base::fold; | |
template <typename T, typename... TArgs> | |
static T fold(T arg, TArgs... args) | |
{ | |
static UnaryOp sUnaryOp; | |
return sUnaryOp(fold(args...), arg); | |
} | |
}; | |
template <typename UnaryOp> | |
struct fold_right_trait : public fold_base | |
{ | |
// bring in the recursive base case from the parent | |
using fold_base::fold; | |
template <typename T, typename... TArgs> | |
static T fold(T arg, TArgs... args) | |
{ | |
static UnaryOp sUnaryOp; | |
return sUnaryOp(arg, fold(args...)); | |
} | |
}; | |
template <typename TFoldDirection, typename T, typename... TArgs> | |
T fold(T arg, TArgs... args) | |
{ | |
return TFoldDirection::fold(arg, args...); | |
} | |
template <typename T, typename... TArgs> | |
T all(T arg, TArgs... args) | |
{ | |
return fold<fold_left_trait<std::logical_and<bool>>>(arg, args...); | |
} | |
//int main(int argc, char** argv) | |
//{ | |
// //bool result = fold<fold_left_trait<std::logical_and<bool>>>(true, true, false, true); | |
// bool result = all(true, true, false, true); | |
// | |
// //std::cout << std::boolalpha << result << std::endl; | |
// | |
// return 0; | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment