imp::unsigned_int< Bits > supports unary and binary operator + () and binary operator += ().
Unary operator + () is a no-op that returns a reference to the object it was called upon.
Binary operator + () has 2 specialization's:
- Addition of two unsigned_int<>'s. Does the addition in the widest (most declared bits) of it's two arguments and returns that type.
- Addition of a unsigned_int<>'s with another numeric type. As (1) but uses std::numeric_limits<> to calculate the widest type.
operator += () has 3 sepecialization's, a more effecient version is used when both arguments are unsigned_int<>'s and uses a more generic version when the other argument is another numeric type, both of these simply convert the second argument to the type of the first argument prior to addition.
imp::unsigned_int< 128 > a( 10 ), b( -1 ), c( 2.0e1 ), d("9");
unsigned u;
c = +a;
std::cout << "unary + of 10: " << c << std::endl;
CHECK( c == 10 && a == 10 );
CHECK( ( c + a ) == 20 );
CHECK( ( c + 20 ) == 30 );
CHECK( ( 20 + c ) == 30 );
c += a;
CHECK( c == 20 );
std::cout
<< "unsigned_int< 128 >(10) += unsigned_int< 128 >(10): "
<< c
<< std::endl
;
u = a;
u += a;
std::cout << "unsigned(10) += unsigned_int< 128 >(10): " << a << std::endl;
CHECK( u == 20 );
u += 10;
CHECK( u == 30 );
u += -10;
CHECK( u == 20 );
Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed
Output
unary + of 10: 10 unsigned_int< 128 >(10) += unsigned_int< 128 >(10): 20 unsigned(10) += unsigned_int< 128 >(10): 10 Ok