fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template <class T>
  5. struct average
  6. {
  7. typedef T InternalType; // default
  8.  
  9. InternalType operator()(const std::vector<T>& v)
  10. {
  11. InternalType vectorSum = 0;
  12. for(unsigned int i = 0; i < v.size(); ++i)
  13. {
  14. vectorSum += v[i];
  15. }
  16. return vectorSum / static_cast<float>(v.size());
  17. }
  18. };
  19.  
  20. template <>
  21. struct average<unsigned char>
  22. {
  23. typedef float InternalType;
  24. };
  25.  
  26. int main(int argc, char *argv[])
  27. {
  28. std::vector<float> floatVector;
  29. std::vector<unsigned char> ucVector;
  30.  
  31. for(unsigned int i = 0; i < 5; ++i)
  32. {
  33. floatVector.push_back(i);
  34. ucVector.push_back(i);
  35. }
  36.  
  37. average<float> floatAverageFunctor;
  38. average<unsigned char> ucAverageFunctor;
  39. std::cout << "Float average: " << floatAverageFunctor(floatVector) << std::endl;
  40. std::cout << "UC average: " << ucAverageFunctor(ucVector) << std::endl;
  41.  
  42. return 0;
  43. }
  44.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:40: error: no match for call to ‘(average<unsigned char>) (std::vector<unsigned char, std::allocator<unsigned char> >&)’
stdout
Standard output is empty