Re: casting to structure

2017-06-24 Thread Igor Shirkalin via Digitalmars-d-learn
On Saturday, 24 June 2017 at 21:41:22 UTC, Moritz Maxeiner wrote: Hi, unfortunately not: - Operator overloading is supported via member functions only [1]. - Corollary: You cannot overload operators for builtin types (i.e. where the cast gets rewritten to `e.opOverloaded` where `e` is a

Re: casting to structure

2017-06-24 Thread Moritz Maxeiner via Digitalmars-d-learn
On Saturday, 24 June 2017 at 21:11:13 UTC, Igor Shirkalin wrote: Is it possible without such a constructor? No. Also, the above works because of the following [1]: Casting a value v to a struct S, when value is not a struct of the same type, is equivalent to: `S(v)` [1]

Re: casting to structure

2017-06-24 Thread Moritz Maxeiner via Digitalmars-d-learn
On Saturday, 24 June 2017 at 20:43:48 UTC, Igor Shirkalin wrote: I'm in trouble with opCast function. Is it possible to cast some (integral) type to user defined structure? Hi, unfortunately not: - Operator overloading is supported via member functions only [1]. - Corollary: You cannot

Re: casting to structure

2017-06-24 Thread Igor Shirkalin via Digitalmars-d-learn
On Saturday, 24 June 2017 at 20:43:48 UTC, Igor Shirkalin wrote: struct A { void * data; // void * type is just for example // no matter what is here } I know that if I add constructor this(int) struct A { void * p; this(int k) { p = cast(void*)k; } } auto a = cast(A)

casting to structure

2017-06-24 Thread Igor Shirkalin via Digitalmars-d-learn
Hello! I'm in trouble with opCast function. Is it possible to cast some (integral) type to user defined structure? We have a structure: struct A { void * data; // void * type is just for example // no matter what is here } How can we define opCast operator to make the following