jeudi 1 juin 2023

Bijectional mapping between two instance of different type

I have two class A and B. There is a bijection between every instances of these class.

class A {
    public:
        constexpr A() {}
};
class B {
    public:
        constexpr B() {}
}

// static delcaration of all pair of equivalent instances.

constexpr A toA(B);
constexpr B toB(A);


int main()
{
    A a;
    B b;
    a == toA(toB(a)); // always true
    b == toB(toA(b)); // always true
}

I need to find an elegant solution to solve this at compile and run time. I've tried many solutions like home-made bidirectional map, template variable etc but none of them matched my expectation.

Aucun commentaire:

Enregistrer un commentaire