To unsubscribe from this group and stop receiving emails from it, send an email to blink-dev+...@chromium.org.
Hi,Just noticed that we can compare a reference to a pointer for some types (e.g. RenderObject). https://codereview.chromium.org/184023003/ etc. added the behavior.The default language behavior of operator==(const T& a, const T& b) is also changed to comparing addresses.
Is this just temporary to avoid too much code change, or allowed (or even preferred?) in future code?
IMHO, the behaviors are bad because it's not the correct way C++ language should work.
IMHO, the behaviors are bad because it's not the correct way C++ language should work.Can you expand on this argument (perhaps give an example where this operator is causing problems)? For all of the types where this has been done in Blink (to my knowledge), there is no other sense of equality than pointer equality. And so it seems perfectly within the realm of C++ to define operator== to test equality in an application-specific way.
Can you supply an example where ptrEquals is safer?
On Tue, Jul 8, 2014 at 3:43 PM, Adam Klein <ad...@chromium.org> wrote:
On Tue, Jul 8, 2014 at 3:40 PM, Peter Kasting <pkas...@google.com> wrote:
On Tue, Jul 8, 2014 at 3:34 PM, Adam Klein <ad...@chromium.org> wrote:
IMHO, the behaviors are bad because it's not the correct way C++ language should work.Can you expand on this argument (perhaps give an example where this operator is causing problems)? For all of the types where this has been done in Blink (to my knowledge), there is no other sense of equality than pointer equality. And so it seems perfectly within the realm of C++ to define operator== to test equality in an application-specific way.I tend to agree that this kind of overload is confusing.I think the Google style guide would suggest that, instead of using an overloaded operator==() here, using an explicitly-named method (e.g. ptrEquals()) would be clearer and safer.
I said clearer, not cleaner.
I definitely think the ptrEquals() call is clearer, functionally, than the use of == in your example above. (isPtrEquivalent() would probably be an even better name.)
Can you supply an example where ptrEquals is safer?It's safer in any case where a reader could imagine (accurately or not) objects being compared by value.
How likely that is depends on a reader's familiarity with the code and types in question. I tend to want to guarantee safety for people who are at a lower familiarity level, since they're the most likely to introduce bugs.
OK, let me fall back on my less-friendly argument.
The Google style guide outright bans what you're doing ( http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Operator_Overloading ). To the degree that Blink is slowly trying to reach some kind of style unity with Chromium over time, I think obeying the Google style guide is important. So I think these operator overloads should be replaced.
I happen to also think that doing so makes these cases easier to understand, but I don't think that should actually matter.
there's some other name I'm supposed to use to check value equality,If they make up a new name, I have to wonder whether
To be extra-clear: The Google style guide is outright harmful in cases
like this. It was written by C programmers and occasionally needs to
be ignored when we want to write maintainable C++ code.
On Tue, Jul 8, 2014 at 4:05 PM, Jeffrey Yasskin <jyas...@chromium.org> wrote:
there's some other name I'm supposed to use to check value equality,If they make up a new name, I have to wonder whether
[...]
To be extra-clear: The Google style guide is outright harmful in cases
like this. It was written by C programmers and occasionally needs to
be ignored when we want to write maintainable C++ code.I would love to have the freedom to just make that argument about lots of things in the Google style guide I don't agree with. However, I think we only get to make that call in exceptionally rare cases where the style guide is egregiously wrong and highly dangerous for some thing we have to be doing. Otherwise, for better or worse, we've tried hard as a project (on the Chromium side) to obey the Google style guide. I definitely do not agree with you that this is a case where violating the style guide makes Blink dramatically more maintainable.
This Google style guide advice doesn't seem particularly well-followed:
/src/chromium/src$ git grep 'operator==' -- \*.h | wc -l289
In general Blink doesn't need to follow the Google style guide though
This seems like a (much) larger, and I doubt whether overloading operator== on a few classes in Blink is the most important divergence between Blink and Google style. Going down the style guide, I see some much bigger fish:
- Multiple inheritance- Reference arguments- Default argumentsIf your real argument against operator== is that Blink should follow the Google style guide, that seems like a separate thread to me.
To answer the OP, the operator== is provided on all types where the concept of value equality and pointer equality are identical.
- E
On Tue, Jul 8, 2014 at 4:25 PM, Elliott Sprehn <esp...@chromium.org> wrote:
We should not add ptrEquals, it just makes the code harder to read,
I agree that adding ptrEquals is silly.To answer the OP, the operator== is provided on all types where the concept of value equality and pointer equality are identical.From a practical standpoint, I wonder how much these operator==s are actually used (especially the surprising reference+pointer variants), versus the unambiguous &foo == &bar (when foo, bar are references), &foo == bar (when foo is a reference, bar is a pointer), etc.The benefit of &foo == &bar (etc.) to non-blinker ignoramuses like me is that it's clear at the comparison site what's being checked. The cost is obviously some extra &s.
The 'need' for the operator only arises because of the widespread use
of non-const refs in Blink, to indicate a non-nullable pointer. I
think this is a mistake for several reasons:
* It violates the Google style guide that does not allow non-const refs
- There are good reasons for this rule eg. that non-const refs are
confusing at the call site.
- Blink is getting more and more non-const refs, so it is moving
_away_ from the Google style guide
* It doesn't work with Oilpan transition types, because you can't
override '.', only '->'
* It leads to these == operators. Because people are thinking of refs
as non-nullable pointers they expect to be able to compare them with
pointers.
* It doesn't actually tell you that the pointer is not null any more
than using -> on a pointer does
The last point perhaps bears elaboration. Consider two versions of a function.
// Ref version.
foo(*b); // Undefined behaviour happens here if b is null.
void foo(Bar& b) // This tells us that b is not null, though actually
in practice it may be.
{
b.m(); // This is where the crash actually happens in practice.
}
// Pointer version.
foo(b);
void foo(Bar* b)
{
b->m(); // This tells us that b is not null, and this is where
undefined behaviour happens if b is null
}
In both cases, if b is actually null, then undefined behaviour is
encountered. In practice the compiler generates the same code in both
cases, and the crash happens in the same place, at the call to m().
There are more and more examples where we create a ref from a pointer
using the * operator because we know it's not null at that point.
> email to blink-dev+unsubscribe@chromium.org.
The 'need' for the operator only arises because of the widespread use
of non-const refs in Blink, to indicate a non-nullable pointer. I
think this is a mistake for several reasons:
* It violates the Google style guide that does not allow non-const refs
- There are good reasons for this rule eg. that non-const refs are
confusing at the call site.
- Blink is getting more and more non-const refs, so it is moving
_away_ from the Google style guide