Bug 156266 - Implement operator== and operator!= for Optional<>
Summary: Implement operator== and operator!= for Optional<>
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Simon Fraser (smfr)
URL:
Keywords:
Depends on:
Blocks: 156274
  Show dependency treegraph
 
Reported: 2016-04-05 17:21 PDT by Simon Fraser (smfr)
Modified: 2016-04-06 12:04 PDT (History)
8 users (show)

See Also:


Attachments
Patch (4.81 KB, patch)
2016-04-05 17:22 PDT, Simon Fraser (smfr)
andersca: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Fraser (smfr) 2016-04-05 17:21:44 PDT
Implement operator== and operator!= for Optional<>
Comment 1 Simon Fraser (smfr) 2016-04-05 17:22:48 PDT
Created attachment 275726 [details]
Patch
Comment 2 WebKit Commit Bot 2016-04-05 17:23:33 PDT
Attachment 275726 [details] did not pass style-queue:


ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:115:  Consider using EXPECT_EQ instead of EXPECT_TRUE(a == b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:116:  Consider using EXPECT_EQ instead of EXPECT_TRUE(a == b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:117:  Consider using EXPECT_NE instead of EXPECT_FALSE(a == b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:118:  Consider using EXPECT_NE instead of EXPECT_FALSE(a == b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:140:  Consider using EXPECT_EQ instead of EXPECT_FALSE(a != b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:141:  Consider using EXPECT_NE instead of EXPECT_TRUE(a != b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:142:  Consider using EXPECT_EQ instead of EXPECT_FALSE(a != b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:143:  Consider using EXPECT_NE instead of EXPECT_TRUE(a != b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:145:  Consider using EXPECT_NE instead of EXPECT_TRUE(a != b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:146:  Consider using EXPECT_NE instead of EXPECT_TRUE(a != b)  [readability/check] [2]
Total errors found: 10 in 4 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Myles C. Maxfield 2016-04-05 17:28:33 PDT
Comment on attachment 275726 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=275726&action=review

I think I should wait for Anders to give the r+, but LGTM.

> Source/WTF/wtf/Optional.h:202
> +    return static_cast<bool>(lhs) == static_cast<bool>(rhs) && (!static_cast<bool>(lhs) || lhs.value() == rhs.value());

Wouldn't an if statement be cleaner?

Are these taken directly from C++ docs?

> Source/WTF/wtf/Optional.h:256
> +    return !(value == opt);

Perhaps it would be useful to make one with two template types, and static_cast one to the other, to try to compare things which aren't the same type but are convertible.
Comment 4 Simon Fraser (smfr) 2016-04-05 17:31:30 PDT
(In reply to comment #2)
> Attachment 275726 [details] did not pass style-queue:
> 
> 
> ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:115:  Consider using
> EXPECT_EQ instead of EXPECT_TRUE(a == b)  [readability/check] [2]

Using these didn't compile.(In reply to comment #3)

> Comment on attachment 275726 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=275726&action=review
> 
> I think I should wait for Anders to give the r+, but LGTM.
> 
> > Source/WTF/wtf/Optional.h:202
> > +    return static_cast<bool>(lhs) == static_cast<bool>(rhs) && (!static_cast<bool>(lhs) || lhs.value() == rhs.value());
> 
> Wouldn't an if statement be cleaner?

But this is branchless.
> 
> Are these taken directly from C++ docs?

Some are.
Comment 5 Anders Carlsson 2016-04-06 11:35:18 PDT
(In reply to comment #4)
> (In reply to comment #2)
> > Attachment 275726 [details] did not pass style-queue:
> > 
> > 
> > ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:115:  Consider using
> > EXPECT_EQ instead of EXPECT_TRUE(a == b)  [readability/check] [2]
> 
> Using these didn't compile.(In reply to comment #3)
> 
> > Comment on attachment 275726 [details]
> > Patch
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=275726&action=review
> > 
> > I think I should wait for Anders to give the r+, but LGTM.
> > 
> > > Source/WTF/wtf/Optional.h:202
> > > +    return static_cast<bool>(lhs) == static_cast<bool>(rhs) && (!static_cast<bool>(lhs) || lhs.value() == rhs.value());
> > 
> > Wouldn't an if statement be cleaner?
> 
> But this is branchless.

Pretty sure it isn't branchless. && and || always form branches. Besides, the compiler will be able to get rid of unnecessary branches - even if you use if statements.

> > 
> > Are these taken directly from C++ docs?
> 
> Some are.
Comment 6 Simon Fraser (smfr) 2016-04-06 12:04:00 PDT
http://trac.webkit.org/changeset/199107