Theodore V. Tolstoy wrote in news:7bec5a7a.0410032226.309b1366
@posting.google.com in comp.lang.c++:
Any comment?
Yep, please post without the line no's, it hinders cut and paste :).
Anyway, John Harrison has already told you the problem, here's
a possible solution:
#include <iostream>
template < typename T, int S >
struct CTest;
class CTest_ops
{
template < typename T, int S >
friend struct CTest;
template< typename T, int S1, int S2 >
struct operator_plus
{
CTest<T, S1> const & a;
CTest<T, S2> const & b;
operator_plus( CTest<T, S1> const &aa, CTest<T, S2> const &bb ) :
a( aa ), b( bb )
{
}
};
template < typename T, int S1, int S2 >
friend CTest_ops::operator_plus< T, S1, S2 >
operator + ( CTest< T, S1 > const &a, CTest< T, S2 > const &b );
};
template < typename T, int S1, int S2 >
CTest_ops::operator_plus< T, S1, S2 >
operator + ( CTest< T, S1 > const &a, CTest< T, S2 > const &b )
{
return CTest_ops::operator_plus< T, S1, S2 >( a, b );
}
template < typename T, int S >
struct CTest
{
T data;
CTest( T const &a ) : data( a ) {}
CTest() {}
template < int S2, int S3 >
CTest operator = ( CTest_ops::operator_plus< T, S2, S3 > const &p )
{
data = p.a.data + p.b.data;
return *this;
}
};
int main()
{
CTest< int, 1> t1( 1 );
CTest< int, 2> t2( 2 );
CTest< int, 3> t3;
t3 = t1 + t2;
std::cout << t3.data << '\n';
}
Rob.
--
http://www.victim-prime.dsl.pipex.com/