tags:

views:

343

answers:

2

I'm building a matrix template. There are operators, functions and all work fine. Except when I try to convert a double type matrix to int type matrix (or vice versa). = operator cannot be defined so its not possible to override it for basic_Matrix2D and basic_Matrix2D external to class. I know I can write in class = operators to convert from but in this case there will be two = operator with same parameters. When using double as template parameter converting from double will be the same as converting from template parameter. Class definition is follows, codes can be accessed from SourceForge

template <class _T> class basic_Matrix2D {

}

There is also another problem I remembered about templates, converting to template type pointer works while converting to template type does not. This might be compiler specific. observe:

operator _T() {
   return something;
}

operator _T*() {
   return somethingelse;
}

Thanks in advance,
Cem

+4  A: 

Your question is very unclear, but theres nothing wrong with making the operator= something like this:

// incomplete, but you get the idea
template<class U>
basic_Matrix2D<T> & operator=(const basic_Matrix2D<U> &x)
{
    rows = x.rows;
    cols = x.cols;
    data = new T[rows * cols];
    for (size_t i = 0; i < rows * cols; ++i)
        data[i] = x.data[i];
}

This will allow you to assign from any matrix where the expression T t; t = U(); is well formed. And if you can't, it will fail to compile. You can also include a simple basic_Matrix2D<T> & operator=(const basic_Matrix2D<T> &); assignment operator as well - maybe you can get some additional efficiency or something out of it.

Greg Rogers
Thx for the clear answer, this is what I'm looking for.
Cem Kalyoncu
+1  A: 

I think it wouldn't be uncommon to provide a "conversion constructor":

template <class U>
Matrix2D<T>(const Matrix2D<U>&)

This should automatically make everything work, e.g

Matrix2D<double> a;
Matrix2D<int> b;
//implicitly uses the conversion constructor to create a temporary Matrix2D<double> from b
a += b;

Even though if making the temporary object would be too expensive, you can define all operators so that the operands can be different types of matrixes.

Conversion operators however are a dead end. They just muddy the water.

P.S I also took a look at the references of your project. What is the purpose of reinventing a linked list, particularly if its interface is completely non-compatible with SC++L? (Even an ancient library like wxWidgets has provided an alternative and preferred STL-compatible interface for their wxString.)


Edit: As to your conversion operator problem, which compiler doesn't it compile with?

template <class T>
struct X
{
    int n;

    operator T() { return n; }
    operator T*() { return &n; }
};

int main()
{
    X<int> x = {42};
    int a = x;
    int* b = x;
}

Note that this might be rather considered an abuse of operator overloading which will not definitely not make your code cleaner.

UncleBens
Thx for the answer. For the linkedlist, it is actually a sorted linked list (insertation sorted) which I was required for another speed-critical project, just imported there for the sake of laziness. For STL-compatibility, I really feel guilty about it but it was not possible to implement a sort function while the list is already sorted therefore no meaning in continuing compatibility.
Cem Kalyoncu
For conversion, I use internal compiler of VC++ 8.0 I also have access to g++ which I yet to check. It actually gives a funny error: 'operator {' is not a recognized operator or type. Actually both conversions (T* and T) are not in the same class.
Cem Kalyoncu