digitalmars.D.learn - pointer to object resolution
- Alex (36/36) May 11 2018 Hi all,
- Steven Schveighoffer (6/9) May 11 2018 operators don't follow pointers.
- Alex (4/14) May 11 2018 Ah!
Hi all,
I'm sure, I didn't find something obvious, but:
Given this:
´´´
void main()
{
auto s = S();
s.operator;
assert(s.myOp(42));
assert(42 in s);
auto sptr = new S();
sptr.operator;
assert(sptr.myOp(42));
//assert(42 in sptr); //<-- does not compile
}
struct S
{
void operator() const
{
assert(true);
}
bool opBinaryRight(string op)(size_t input) const if(op == "in")
{
return true;
}
bool myOp(size_t input)
{
return input in this;
}
}
´´´
The last line in the main does not compile with the message
source/app.d(9,9): Error: incompatible types for `(42) in
(sptr)`: `int` and `S*`
This behaves differently, w.r.t. to an arbitrary method, like
"operator". Why? Is there any workaround?
May 11 2018
On 5/11/18 8:53 AM, Alex wrote:This behaves differently, w.r.t. to an arbitrary method, like "operator". Why? Is there any workaround?operators don't follow pointers. Imagine if you had a struct that overloads "+" and then you wanted to use pointer arithmetic, but instead it called ptr.opBinary. The workaround is to dereference the pointer. e.g. 42 in *sptr; -Steve
May 11 2018
On Friday, 11 May 2018 at 15:24:08 UTC, Steven Schveighoffer wrote:On 5/11/18 8:53 AM, Alex wrote:Ah!This behaves differently, w.r.t. to an arbitrary method, like "operator". Why? Is there any workaround?operators don't follow pointers. Imagine if you had a struct that overloads "+" and then you wanted to use pointer arithmetic, but instead it called ptr.opBinary.The workaround is to dereference the pointer. e.g. 42 in *sptr; -SteveThanks a lot!
May 11 2018








Alex <sascha.orlov gmail.com>