I'm trying to figure out how to add typecast operator to the following nested class to allow the main to compile, but I can't figure what is needed.The next to last assignment in main is causing the problem. Note that the very last assignment uses type casting to make it work. I suspect I need to define either a a typecast-operator for the 'add' class, but how?.
Sorry for the long listing, but this is as simple as I know how to make it.
#include <iostream>
using namespace std;
template <char I> struct index { };
template <char I, char J> struct marker;
// simple class with just a 2 element int array
struct A {
  A() { }
  A(int i, int j) { a[0] = i; a[1] = j; }
  A(const A& x) { a[0] = x(0); a[1] = x(1); }
  template<char I, char J>
  marker<I,J> operator()(const index<I>&, const index<J>&) {
    return marker<I,J>(*this);
  }
  friend std::ostream& operator<<(std::ostream& os, const A& _A) {
    return os << '{' << _A.a[0] << ',' << _A.a[1] << '}';
  }
  int& operator()(int i) { return a[i]; }
  const int& operator()(int i) const { return a[i]; }
private:
  int a[2];
};
template <char I, char J>
struct marker {
  const int DI;
  const int DJ;
  marker(A& a) : _A(a), DI(1), DJ(0) { }
  marker(A& a, const int i, const int j) : _A(a), DI(i), DJ(j) { }
  marker(const marker& m) : _A(m._A), DI(m.DI), DJ(m.DJ) { }
  // cast I,J => J,I
  operator marker<J,I>() const {
    return marker<J,I>(_A, DJ, DI);
  }
  marker& operator=(const marker& m) {
    _A(0) = m(0);
    _A(1) = m(1);
    return *this;
  }
  // returns the i'th or (1-i)'th element of _A
  int operator()(int i) const {
    return _A(i*DI + (1-i)*DJ);
  }
  template<class LHS, class RHS>
  struct add {
    const LHS& lhs;
    const RHS& rhs;
    add(const LHS& l, const RHS& r) : lhs(l), rhs(r) { }
    int operator()(int i) const {
      return lhs(i) + rhs(i);
    }
    add< add,marker > operator+(const marker& b) {
      return add< add,marker >(*this, b);
    }
  };
  add< marker,marker > operator+(const marker& b) const {
    return add< marker,marker >(*this,b);
  }
  template<class LHS>
  void operator=(const add<LHS,marker>& expr) {
    _A(0) = expr(0);
    _A(1) = expr(1);
  }
private:
  A& _A;
};
int main() {
  index<'i'> i;
  index<'j'> j;
  A a(1,2), b;
  b(i,j) = a(j,i);
  cout << b << endl; // "{2,1}"
  b(i,j) = a(i,j) + a(j,i);
  cout << b << endl;  // "{3,3}"
  b(i,j) = a(j,i) + a(i,j); // fails to compile
  cout << b << endl;  // should be "3,3"
  b(i,j) = (marker<'i','j'>)a(j,i) + a(i,j); // works fine
  cout << b << endl; // "{3,3}"
  return 0;
}
 
     
    