Skip to content
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

adjust associative containers to support heterogeneous lookup #972

Open
DHowett-MSFT opened this issue May 24, 2019 · 3 comments
Open

adjust associative containers to support heterogeneous lookup #972

DHowett-MSFT opened this issue May 24, 2019 · 3 comments
Labels
Area-CodeHealth Issues related to code cleanliness, linting, rules, warnings, errors, static analysis, etc. Help Wanted We encourage anyone to jump in on these. Issue-Task It's a feature request, but it doesn't really need a major design. Product-Terminal The new Windows Terminal.
Milestone

Comments

@DHowett-MSFT
Copy link
Contributor

Heterogeneous lookup has been supported since C++14. To quote the linked article,

C++14 allows the lookup to be done via an arbitrary type, so long as the comparison operator can compare that type with the actual key type. This would allow a map from std::string to some value to compare against a const char* or any other type for which an operator< overload is available. It is also useful for indexing composite objects in a std::set by the value of a single member without forcing the user of find to create a dummy object (for example creating an entire struct Person to find a person by name).

Now, it's magic in that you enable it by specifying std::less<> as the third template argument to an associative container; that is:

std::map<std::string, std::string             > map1;
std::map<std::string, std::string, std::less<>> map2;

map2 supports heterogeneous lookup, whereas map1 does not.

@DHowett-MSFT DHowett-MSFT added Product-Terminal The new Windows Terminal. Issue-Task It's a feature request, but it doesn't really need a major design. Area-CodeHealth Issues related to code cleanliness, linting, rules, warnings, errors, static analysis, etc. labels May 24, 2019
@ghost ghost added the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label May 24, 2019
@DHowett-MSFT DHowett-MSFT added Help Wanted We encourage anyone to jump in on these. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting and removed Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels May 24, 2019
@fcharlie
Copy link
Contributor

If you want to use a hash map that supports heterogeneous lookups, there are references below:

@dlong11
Copy link

dlong11 commented May 24, 2019

Are you thinking something like this for stirng keys?

struct StringComparison
{
using is_transparent = void;
bool operator()(std::string_view a, std:string_view b) const
{
return a < b;
}
...
};

std::map<std::string, std::string,, StringComparison> m = <map....>`

@fcharlie
Copy link
Contributor

@DHowett-MSFT Bartek has written an article that may be helpful to you:
Heterogeneous Lookup in Ordered Containers, C++14 Feature

@DHowett-MSFT DHowett-MSFT removed the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label May 28, 2019
@zadjii-msft zadjii-msft added this to the Terminal Backlog milestone Jun 19, 2019
@zadjii-msft zadjii-msft modified the milestones: Terminal Backlog, Backlog Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-CodeHealth Issues related to code cleanliness, linting, rules, warnings, errors, static analysis, etc. Help Wanted We encourage anyone to jump in on these. Issue-Task It's a feature request, but it doesn't really need a major design. Product-Terminal The new Windows Terminal.
Projects
None yet
Development

No branches or pull requests

4 participants