1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| // g++ -Wall -Wextra -std=c++11 operator_typedef.cpp -o operator_typedef && operator_typedef
#include <iostream>
#include <tuple>
template <class T>
using x_y_z = std::tuple<T, T, T>;
template <class T>
x_y_z<T> operator+(x_y_z<T> const & a, x_y_z<T> const & b)
{
return (x_y_z<T>(std::get<0>(a) + std::get<0>(b), std::get<1>(a) + std::get<1>(b), std::get<2>(a) + std::get<2>(b)));
}
template <class T>
std::ostream & operator<<(std::ostream & o, x_y_z<T> const & v)
{
o << "{" << std::get<0>(v) << ", " << std::get<1>(v) << ", " << std::get<2>(v) << "}";
return o;
}
int main()
{
x_y_z<float> a(3.2, 2.2, 1.0);
x_y_z<float> b(3.2, 4.5, 1.4);
std::cout << " " << a << std::endl;
std::cout << "+ " << b << std::endl;
std::cout << "= " << a + b << std::endl;
std::cout << std::endl;
std::tuple<float, float, float> ta(3.2, 2.2, 1.0);
std::tuple<float, float, float> tb(3.2, 4.5, 1.4);
std::cout << " " << ta << std::endl;
std::cout << "+ " << tb << std::endl;
std::cout << "= " << ta + tb << std::endl;
std::cout << std::endl;
return 0;
} |
Partager