fork download
  1. #include <iostream>
  2.  
  3. template<typename T, typename U>
  4. decltype(T() + U()) add(T a, U b)
  5. {
  6. return a + b;
  7. };
  8.  
  9. template<class T1, class T2>
  10. decltype(T1() * T2()) f(T1 v1[], T2 v2[], size_t count) // Will not compile!
  11. {
  12. decltype(v1[0]*v2[0]) sum(0);
  13. for(size_t i = 0; i<count; i++) sum += v1[i]*v2[i];
  14. return sum;
  15. };
  16.  
  17. int main()
  18. {
  19. int i = 9;
  20. int ia[] = {1,2,3,4,5};
  21. double da[] = {1.0,2.0,3.0,4.0,5.0};
  22.  
  23. double d = 2.0;
  24.  
  25. auto a = add<int, double>(i,d);
  26. auto a2 = f<int,double>(ia,da, 5);
  27.  
  28. std::cout << a << std::endl;
  29. std::cout << a2 << std::endl;
  30. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
11
55