Implicit cast of int to a class

Matthias Walter walter at mail.math.uni-magdeburg.de
Sat Nov 3 10:12:29 PDT 2007


Hello,

I have a class called mpz_class which in particular implements the following:

1. this (int other) { ... }
2. opAssign (int other) { ... }

I now want to write templated code with T = int, T = long and T = mpz_class.
My mpz_class should work and feel like the orginal integers, which mostly works.

I'd like to know, how I can coax my class to implicitely convert integer literals to instances of it. There are 2 cases which don't compile for me and one which is semantically wrong:

// 1.
void func (T) (T input = 0) // error:  cannot implicitly convert expression (0) of type int to gmp.mpz.mpz_class
{
  // ...
}

//...

// 2. 
T a = 1; // I guess, a points to 0x1 now and not to a result of mpz_class.this(1)

// 3.
func !(T) (a); // works
func !(T) (1); // error: function func1!(mpz_class).func1 (mpz_class) does not match parameter types (int)

Can someone help me here? 

For full sourcecode, see:

http://dsource.org/projects/gmp4d/browser/trunk/Test.d


More information about the Digitalmars-d-learn mailing list