Advertisement
fcamuso

C++ 20 - lezione 9

Nov 6th, 2022
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <concepts>
  3. #include <format>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T>
  8. concept Number = integral<T> || floating_point<T>;
  9.  
  10. template <typename T1, typename T2>
  11. concept Number2 = integral<T1> && floating_point<T2>;
  12.  
  13. template <typename T1, typename T2>
  14. requires Number2<T1, T2>
  15. T1 somma2(T1 a, T2 b) {
  16.     return a + b;
  17. }
  18.  
  19. template <typename T>
  20.  
  21. T somma(T a, T b) requires Number<T> {
  22.     return a + b;
  23. }
  24.  
  25. struct Punto3D {
  26.     int x = 0;
  27.     int y = 0;
  28.     int z = 0;
  29. };
  30.  
  31. struct Punto2D {
  32.     int x = 0;
  33.     int y = 0;
  34.  
  35.     Punto2D operator +(Punto3D &altro) const
  36.     {
  37.         return Punto2D{x+altro.x, y+altro.y};
  38.     }
  39. };
  40.  
  41.  
  42.  
  43. int main()
  44. {
  45.     Punto2D p1 { 5, 10 };
  46.     Punto3D p2{ 4, -9, 12 };
  47.  
  48.     /*auto p3 = somma2<Punto2D, Punto3D>(p1, p2);
  49.     cout << format("Punto risultato: ({},{})\n", p3.x, p3.y);*/
  50.  
  51.     cout << somma<int>(5, 3) << endl;
  52.     cout << somma<double>(3.15, 56) << endl;
  53.  
  54.  
  55.     //cout << somma2<int>(5, 3) << endl;
  56.     //cout << somma2<double>(3.15, 56) << endl;
  57.     //cout << somma2<double>(56, 3.15) << endl;
  58.     cout << somma2(56, 3.15) << endl;
  59.     cout << somma2<int>(56, 3.15) << endl;
  60.     cout << somma2<int, double>(56, 3.15) << endl;
  61.  
  62.   //cout << somma<string>("ciao", " a tutti") << endl;
  63.  
  64.    
  65.  
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement