-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc++] Make sure flat_{multi}map::key_compare
handle boolean-testable
correctly
#132621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hewillk
wants to merge
5
commits into
llvm:main
Choose a base branch
from
hewillk:main-flat-map-compare
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e0f56c0
[libc++] Make sure compare handle boolean-testable correctly
hewillk 436ae2e
[libc++] Make sure compare handle boolean-testable correctly
hewillk 393d9e6
Added Unit Tests
huixie90 986530c
CI
huixie90 0e5a7dd
Merge branch 'llvm:main' into main-flat-map-compare
hewillk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...x/test/std/containers/container.adaptors/flat.map/robust_against_nonbool.compile.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 | ||
|
||
// <flat_map> | ||
// | ||
// flat_map should support comparator that return a non-boolean | ||
// value as long as the returned type is implicitly convertible to bool. | ||
|
||
#include <flat_map> | ||
#include <vector> | ||
#include <ranges> | ||
|
||
#include "boolean_testable.h" | ||
|
||
void test() { | ||
using Key = StrictComparable<int>; | ||
using Value = StrictComparable<int>; | ||
std::flat_map<Key, Value> m1; | ||
std::flat_map m2(std::from_range, m1, StrictBinaryPredicate); | ||
std::flat_map m3(std::sorted_unique, m1.keys(), m1.values(), StrictBinaryPredicate); | ||
std::flat_map m4(m1.begin(), m1.end(), StrictBinaryPredicate); | ||
m2.insert(m1.begin(), m1.end()); | ||
m2.insert(std::sorted_unique, m1.begin(), m1.end()); | ||
m2.insert_range(m1); | ||
(void)m2.at(2); | ||
m3[1] = 2; | ||
m3.insert_or_assign(1, 2); | ||
m4.try_emplace(1, 2); | ||
m2.emplace(1, 2); | ||
m2.emplace_hint(m2.begin(), 1, 2); | ||
for (const auto& [k, v] : m2) { | ||
(void)k; | ||
(void)v; | ||
} | ||
(void)m2.find(Key{1}); | ||
(void)m2.equal_range(Key{1}); | ||
(void)(m2 == m2); | ||
m2.erase(m2.begin()); | ||
m2.erase(m2.begin(), m2.end()); | ||
std::erase_if( | ||
m2, []<class T>(std::pair<const StrictComparable<T>&, const StrictComparable<T>&>) -> BooleanTestable const& { | ||
return yes; | ||
}); | ||
} |
47 changes: 47 additions & 0 deletions
47
...t/std/containers/container.adaptors/flat.multimap/robust_against_nonbool.compile.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 | ||
|
||
// <flat_map> | ||
// | ||
// flat_multimap should support comparator that return a non-boolean | ||
// value as long as the returned type is implicitly convertible to bool. | ||
|
||
#include <flat_map> | ||
#include <ranges> | ||
#include <vector> | ||
|
||
#include "boolean_testable.h" | ||
|
||
void test() { | ||
using Key = StrictComparable<int>; | ||
using Value = StrictComparable<int>; | ||
std::flat_multimap<Key, Value> m1; | ||
std::flat_multimap m2(std::from_range, m1, StrictBinaryPredicate); | ||
std::flat_multimap m3(std::sorted_equivalent, m1.keys(), m1.values(), StrictBinaryPredicate); | ||
std::flat_multimap m4(m1.begin(), m1.end(), StrictBinaryPredicate); | ||
m2.insert(m1.begin(), m1.end()); | ||
m2.insert(std::sorted_equivalent, m1.begin(), m1.end()); | ||
m2.insert_range(m1); | ||
m2.emplace(1, 2); | ||
m2.emplace_hint(m2.begin(), 1, 2); | ||
for (const auto& [k, v] : m2) { | ||
(void)k; | ||
(void)v; | ||
} | ||
(void)m2.find(Key{1}); | ||
(void)m2.equal_range(Key{1}); | ||
(void)(m2 == m2); | ||
m2.erase(m2.begin()); | ||
m2.erase(m2.begin(), m2.end()); | ||
std::erase_if( | ||
m2, []<class T>(std::pair<const StrictComparable<T>&, const StrictComparable<T>&>) -> BooleanTestable const& { | ||
return yes; | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's expected that
decltype(__compare_(__x, __y))
already modelboolean-testable
(or even is exactlybool
in most normal usages).If the standard were already so requiring, we wouldn't need to change these lambdas. But I haven't found such a requirement...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I was likely to be wrong. But are such conversions already performed in algorithms?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://godbolt.org/z/nbG9v4zqY
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see. It's the nature of lambda expression without trailing return type that unfortunately decay-copies the result.
Perhaps there should be some test cases for this, with types in
boolean_testable.h
reused?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a local repository for llvm, co-working is welcome.