4 ms·
>I have yet to see any of this be an actual problem in real-world projects Just on Monday, I was bitten by a bug caused by operator overloading. I'm building a
by madmax96 5y ago
>I have yet to see any of this be an actual problem in real-world projects
Just on Monday, I was bitten by a bug caused by operator overloading. I'm building a static analysis tool in C++. There's a method `getType()` that returns the qualified type of an expression. I had a call to `getType()` nested in some other calls, e.g. `expr->asAssignmentExpr()->getRHS()->getType()->getSomeQualifiedTypeProperty()`.
The creators of the `QualifiedType` type that's returned by `getType()` had overridden the class member access operator to return an UnqualifiedType. This problem took me an embarrassingly long amount of time to diagnose. The compiler error messages told me that `UnqualifiedType` doesn't have a property `getSomeQualifiedTypeProperty`, but I couldn't see why I was getting an unqualified type from a method that is supposed to return a qualified type. I searched the documentation online, searched the header files, tried reinstalling the library, nothing made sense.
Then, I learned this operator had been overloaded. So, I just needed to write `expr->asAssignmentExpr()->getRHS()->getType().getSomeQualifiedTypeProperty()`.
Ouch.
- jcelerier 5y ago> So, I just needed to write `expr->asAssignmentExpr()->getRHS()->getType().getSomeQualifiedTypeProperty()`. with any decent IDE you'd type something like expr.as<enter>.get<enter>.get<enter>.get<enter> and you'd see the following: https://streamable.com/xib4s0 https://streamable.com/xib4s0 so I fail to see how this can be an issue if you don't use prehistoric tooling