Advertisement
fcamuso

C++ 20 - lezione 10

Nov 16th, 2022
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 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. T somma(T a, T b) requires Number<T> {
  21.     return a + b;
  22. }
  23.  
  24. struct Punto3D {
  25.     int x = 0;
  26.     int y = 0;
  27.     int z = 0;
  28. };
  29.  
  30. struct Punto2D {
  31.     int x = 0;
  32.     int y = 0;
  33.  
  34.     Punto2D operator +(Punto3D &altro) const
  35.     {
  36.         return Punto2D{x+altro.x, y+altro.y};
  37.     }
  38. };
  39.  
  40.  
  41. //forma abbreviata
  42. auto somma3(Number auto n1, Number auto n2)
  43. { return n1+n2; }
  44.  
  45. template <Number T>
  46. class UsaConcept {
  47. public: UsaConcept(T numero) : valore{ numero } {}
  48.     T valore;
  49. };
  50.  
  51.  
  52. template <typename T,typename T2>
  53. concept isGuerriero =
  54. requires(T guerriero, T2 coefficiente) {  
  55.     integral<T2>;
  56.     {guerriero.attacca(coefficiente)} -> floating_point;
  57.     guerriero.para();    
  58. };
  59.  
  60. template<typename G, typename C>
  61. requires isGuerriero<G, C>
  62. class Plotone{
  63.     G soldati[100];
  64. };
  65.  
  66. class Centurione
  67. {
  68. public:
  69.     double attacca(int coefficiente) { return 4.5; }
  70.     void para() {}
  71. };
  72.  
  73. class Legionario {
  74. public:
  75.     void attacca() {}
  76.     void para() {}
  77. };
  78.  
  79. int main()
  80. {
  81.     //OK Centurione soddisfa il concept
  82.     Plotone<Centurione, int> plot1 = Plotone<Centurione, int>();
  83.  
  84.     //Plotone<Legionario> plot2 = Plotone<Legionario>();
  85.  
  86.  
  87.     Punto2D p1 { 5, 10 };
  88.     Punto3D p2{ 4, -9, 12 };
  89.  
  90.     /*auto p3 = somma2<Punto2D, Punto3D>(p1, p2);
  91.     cout << format("Punto risultato: ({},{})\n", p3.x, p3.y);*/
  92.  
  93.     cout << somma<int>(5, 3) << endl;
  94.     cout << somma<double>(3.15, 56) << endl;
  95.  
  96.  
  97.     //cout << somma2<int>(5, 3) << endl;
  98.     //cout << somma2<double>(3.15, 56) << endl;
  99.     //cout << somma2<double>(56, 3.15) << endl;
  100.     cout << somma2(56, 3.15) << endl;
  101.     cout << somma2<int>(56, 3.15) << endl;
  102.     cout << somma2<int, double>(56, 3.15) << endl;
  103.  
  104.   //cout << somma<string>("ciao", " a tutti") << endl;
  105.  
  106.    
  107.  
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement