Skip to content

Instantly share code, notes, and snippets.

@nyuichi
Created March 5, 2015 04:49
Show Gist options
  • Select an option

  • Save nyuichi/d63b371341d38f2795fc to your computer and use it in GitHub Desktop.

Select an option

Save nyuichi/d63b371341d38f2795fc to your computer and use it in GitHub Desktop.
#include <iostream>
#include <type_traits>
#include <typeinfo>
#include <typeindex>
template<typename T> struct Key;
template<> struct Key<int> { static constexpr std::size_t value = 0; };
template<> struct Key<float> { static constexpr std::size_t value = 1; };
template<typename T, typename U>
struct Sort
{
using T1 = typename std::conditional<Key<T>::value <= Key<U>::value, T, U>::type;
using T2 = typename std::conditional<Key<T>::value <= Key<U>::value, U, T>::type;
};
template<typename T, typename U>
struct A
{
T t;
U u;
};
template<typename T, typename U>
struct A_proxy
{
A<typename Sort<T, U>::T1, typename Sort<T, U>::T2> s;
};
int main()
{
A_proxy<int, float> a;
a.s.t = 10;
a.s.u = 0.1;
std::cout << a.s.t << ' ' << a.s.u << std::endl;
A_proxy<float, int> b;
b.s.t = 10;
b.s.u = 0.1;
std::cout << b.s.t << ' ' << b.s.u << std::endl;
a.s = b.s; // same!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment