0

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;
}
gogators
  • 783
  • 2
  • 6
  • 17
  • 2
    This is probably not the cause of your issues, but identifiers beginning with an underscore and a capital letter (like `_A`) are [reserved in any scope](http://stackoverflow.com/a/228797/185171) – David Brown Jan 22 '12 at 23:20
  • Thanks. Did not know that. No complaints from the compiler though in this case. – gogators Jan 23 '12 at 00:49

2 Answers2

1

clang gives the following error:

/tmp/webcompile/_13759_1.cc:97:10: error: no viable overloaded '='
  b(i,j) = a(j,i) + a(i,j); // fails to compile
  ~~~~~~ ^ ~~~~~~~~~~~~~~~
/tmp/webcompile/_13759_1.cc:44:11: note: candidate function not viable: no known conversion from 'add<marker<'j', 'i'>, marker<'j', 'i'> >' to 'const marker<'i', 'j'>' for 1st argument; 
  marker& operator=(const marker& m) {
          ^
/tmp/webcompile/_13759_1.cc:76:8: note: candidate template ignored: failed template argument deduction
  void operator=(const add<LHS,marker>& expr) {
       ^

The error just means the compiler can't find a way to perform the assignment in question. The compiler then lists the two operator= it tried. The first obviously fails to match. For the second, failed template argument deduction just means that the compiler can't figure out how to convert the argument to a const add<LHS,marker>& for any type LHS. Perhaps you want the following?

template<class LHS, class RHS>
void operator=(const add<LHS,RHS>& expr)

EDIT: If you explicitly call the operator= and force the template parameters, the issue becomes more obvious:

/tmp/webcompile/_28787_0.cc:95:10: error: no matching member function for call to 'operator='
  b(i,j).operator=<marker<'j', 'i'>, marker<'j', 'i'> >(a(j,i) + a(i,j)); // fails to compile
  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/webcompile/_28787_0.cc:76:6: note: candidate function [with LHS = marker<'j', 'i'>, RHS = marker<'j', 'i'>] not viable: no known conversion from 'add<marker<'j', 'i'>, marker<'j', 'i'> >' (aka 'marker<'j', 'i'>::add<marker<'j', 'i'>, marker<'j', 'i'> >') to 'add<marker<'j', 'i'>, marker<'j', 'i'> >' (aka 'marker<'i', 'j'>::add<marker<'j', 'i'>, marker<'j', 'i'> >') for 1st argument; 
void operator=(add<LHS,RHS>);
     ^

Specifically, the parameter is marker<'i', 'j'>::add, and the argument is marker<'j', 'i'>::add.

servn
  • 3,049
  • 14
  • 8
  • Figured it was something like that. Your suggestion, though more general, also gives a similar error. – gogators Jan 23 '12 at 00:51
  • Also, if you change `b(i,j)` to `b(j,i)` on the line that gives you an error, the parameter becomes `marker<'j', 'i'>::add` and the code compiles. However, I wonder what the solution would be. – Jesse Good Jan 23 '12 at 06:27
0

[SOLVED] OK, figured it out. All I had to do was add the following member to class marker:

  template <class LHS>
  void operator=(const typename marker<J,I>::template add< LHS, marker<J,I> >& expr) {
    myA(0) = expr(1);
    myA(1) = expr(0);
  }

The syntax of the parameter specification is what was messing me up. servn's gave me the clue and some trial and error got the syntax correct.

Community
  • 1
  • 1
gogators
  • 783
  • 2
  • 6
  • 17