Don't like ads? PRO users don't see any ads ;-)
Guest

variadic template

By: mizonokuchi on May 1st, 2012  |  syntax: C++  |  size: 0.57 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename Op, typename T>
  6. T accumulate(const Op& _operator, const T& a, const T& b){
  7.   return _operator(a, b);
  8. }
  9.  
  10. template <typename Op, typename T, typename ... Types>
  11. T accumulate(const Op& _operator, const T& head, Types ... tail){
  12.   return accumulate(_operator, head, accumulate(_operator, tail...));
  13. }
  14.  
  15.  
  16. class my_operator{
  17. public:
  18.   int operator ()(int a, int b) const{
  19.     return a + b;
  20.   }
  21. };
  22.  
  23. int main(){
  24.   cout << accumulate(my_operator(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << endl;
  25.  
  26.   return 0;
  27. }