fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <type_traits>
  4.  
  5. template<typename T>
  6. struct Typer { typedef T type; };
  7.  
  8. template<typename T>
  9. struct Typer<const T> { typedef T type; };
  10.  
  11. template<typename Iter>
  12. void sumout(Iter first, Iter last) {
  13. typedef typename Typer<typename Iter::value_type>::type Value;
  14.  
  15. Value v = 0;
  16. for (; first != last; (void) ++first) { v += *first; }
  17.  
  18. bool cv = std::is_const<Value>::value;
  19. std::cout << (cv ? "const " : "") << typeid(v).name() << ": " << v;
  20. }
  21.  
  22. int main() {
  23. std::vector<int> v = { 1, 2, 3 };
  24. sumout(v.cbegin(), v.cend());
  25. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
i: 6