42 lines
778 B
C++
42 lines
778 B
C++
#include <iostream>
|
|
|
|
template <typename T>
|
|
int get_at (const T&, int);
|
|
|
|
struct XY {
|
|
int x, y;
|
|
};
|
|
|
|
template <> int get_at(const int(&in)[2], int i) { return in[i]; }
|
|
template <> int get_at(const XY& in, int i) { return (i ? in.y : in.x); }
|
|
|
|
template <typename T>
|
|
struct Vec {
|
|
int x() const { return get_at(m_storage_type, 0); }
|
|
int y() const { return get_at(m_storage_type, 1); }
|
|
|
|
T m_storage_type;
|
|
};
|
|
|
|
template <typename T>
|
|
std::ostream& operator<< (std::ostream& os, const Vec<T>& v) {
|
|
os << '<' << v.x() << ',' << v.y() << '>';
|
|
return os;
|
|
}
|
|
|
|
int main() {
|
|
Vec<int[2]> v1;
|
|
Vec<XY> v2;
|
|
|
|
v1.m_storage_type[0] = 1234;
|
|
v1.m_storage_type[1] = 7777;
|
|
|
|
v2.m_storage_type.x = 44;
|
|
v2.m_storage_type.y = 9999;
|
|
|
|
std::cout << v1 << '\n';
|
|
std::cout << v2 << '\n';
|
|
|
|
return 0;
|
|
}
|