Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <concepts>
- #include <format>
- using namespace std;
- template <typename T>
- concept Number = integral<T> || floating_point<T>;
- template <typename T1, typename T2>
- concept Number2 = integral<T1> && floating_point<T2>;
- template <typename T1, typename T2>
- requires Number2<T1, T2>
- T1 somma2(T1 a, T2 b) {
- return a + b;
- }
- template <typename T>
- T somma(T a, T b) requires Number<T> {
- return a + b;
- }
- struct Punto3D {
- int x = 0;
- int y = 0;
- int z = 0;
- };
- struct Punto2D {
- int x = 0;
- int y = 0;
- Punto2D operator +(Punto3D &altro) const
- {
- return Punto2D{x+altro.x, y+altro.y};
- }
- };
- //forma abbreviata
- auto somma3(Number auto n1, Number auto n2)
- { return n1+n2; }
- template <Number T>
- class UsaConcept {
- public: UsaConcept(T numero) : valore{ numero } {}
- T valore;
- };
- template <typename T,typename T2>
- concept isGuerriero =
- requires(T guerriero, T2 coefficiente) {
- integral<T2>;
- {guerriero.attacca(coefficiente)} -> floating_point;
- guerriero.para();
- };
- template<typename G, typename C>
- requires isGuerriero<G, C>
- class Plotone{
- G soldati[100];
- };
- class Centurione
- {
- public:
- double attacca(int coefficiente) { return 4.5; }
- void para() {}
- };
- class Legionario {
- public:
- void attacca() {}
- void para() {}
- };
- int main()
- {
- //OK Centurione soddisfa il concept
- Plotone<Centurione, int> plot1 = Plotone<Centurione, int>();
- //Plotone<Legionario> plot2 = Plotone<Legionario>();
- Punto2D p1 { 5, 10 };
- Punto3D p2{ 4, -9, 12 };
- /*auto p3 = somma2<Punto2D, Punto3D>(p1, p2);
- cout << format("Punto risultato: ({},{})\n", p3.x, p3.y);*/
- cout << somma<int>(5, 3) << endl;
- cout << somma<double>(3.15, 56) << endl;
- //cout << somma2<int>(5, 3) << endl;
- //cout << somma2<double>(3.15, 56) << endl;
- //cout << somma2<double>(56, 3.15) << endl;
- cout << somma2(56, 3.15) << endl;
- cout << somma2<int>(56, 3.15) << endl;
- cout << somma2<int, double>(56, 3.15) << endl;
- //cout << somma<string>("ciao", " a tutti") << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement