|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r66188 - sandbox/variadic_templates/sandbox
From: cppljevans_at_[hidden]
Date: 2010-10-25 14:56:45
Author: cppljevans
Date: 2010-10-25 14:56:42 EDT (Mon, 25 Oct 2010)
New Revision: 66188
URL: http://svn.boost.org/trac/boost/changeset/66188
Log:
Use functor instead of function pointer:
/home/evansl/download/gcc/4.5.1-release/install/bin/g++ -Wl,-rpath /home/evansl/download/gcc/4.5.1-release/install/lib64 /home/evansl/prog_dev/boost-svn/ro/trunk/sandbox-local/build/gcc4_5_1v/boost-svn/ro/sandbox/rw/variadic_templates/sandbox/varbin.o -o /home/evansl/prog_dev/boost-svn/ro/trunk/sandbox-local/build/gcc4_5_1v/boost-svn/ro/sandbox/rw/variadic_templates/sandbox/varbin.exe
/home/evansl/prog_dev/boost-svn/ro/trunk/sandbox-local/build/gcc4_5_1v/boost-svn/ro/sandbox/rw/variadic_templates/sandbox/varbin.exe
1
6
60
Compilation finished at Mon Oct 25 13:55:29
Text files modified:
sandbox/variadic_templates/sandbox/varbin.cpp | 40 ++++++++++++++++++++++++++++++----------
1 files changed, 30 insertions(+), 10 deletions(-)
Modified: sandbox/variadic_templates/sandbox/varbin.cpp
==============================================================================
--- sandbox/variadic_templates/sandbox/varbin.cpp (original)
+++ sandbox/variadic_templates/sandbox/varbin.cpp 2010-10-25 14:56:42 EDT (Mon, 25 Oct 2010)
@@ -2,21 +2,41 @@
// see if variadic reduce is possible using variadic templates.
//
//Result:
-// // No, apparently b... is not an arg pack.
-// //
-// int add(int a,int b){return a+b;}
+// Yes.
+//
+
+struct add
+{
+ template<typename T>
+ T
+ operator()(T a,T b)
+ {
+ return a+b;
+ }
-template<typename T, typename... U>
-T fold(T (*op)(T,T), T z, U... a)
+};
+
+template<typename Op, typename T, typename... U>
+T fold(Op op, T z, U... a)
;
-template<typename T>
-T fold(T (*op)(T,T), T z, T a)
-{ return op(z,a)
+template<typename Op, typename T>
+T fold(Op op, T z)
+{ return z
;}
-template<typename T, typename... U>
-T fold(T (*op)(T,T), T z, T a, U... b)
+template<typename Op, typename T, typename... U>
+T fold(Op op, T z, T a, U... b)
{ return op(z,fold(op,a,b...))
;}
+#include <iostream>
+
+int main(void)
+{
+ add op;
+ std::cout<<fold(op,1)<<"\n";
+ std::cout<<fold(op,1,2,3)<<"\n";
+ std::cout<<fold(op,10,20,30)<<"\n";
+ return 0;
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk