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

Question: Hashmap as template parameter to another class #256

Closed
lczech opened this issue Oct 22, 2024 · 2 comments
Closed

Question: Hashmap as template parameter to another class #256

lczech opened this issue Oct 22, 2024 · 2 comments

Comments

@lczech
Copy link

lczech commented Oct 22, 2024

I am trying to write a class template that accepts a Hashmap as a template parameter, but in a way where the key and value types do not have to be defined, and instead are set by the class:

template<template<typename, typename, typename...> class Hashmap = std::unordered_map>
class MyClass {
public:
    using MapType = Hashmap<std::string, int>;
    MapType my_map;
};

in order to allow for easy usage (user does not have to provide std::string and int), and because the key/value types are meant to be internal to the class anyway.

In other words, I'd like the class to be usable like this:

MyClass<std::unordered_map> obj1;
MyClass<phmap::parallel_flat_hash_map> obj2;

This however fails to compile for parallel_flat_hash_map, as it contains a template parameter size_t N = 4, and non-type parameters are not allowed in variadic template template parameters...

Is there a way to get this to work, or do I have to change the template arguments of MyClass and have the user specify

MyClass<phmap::parallel_flat_hash_map<std::string, int>> obj3;

explicitly each time?

Thank you in advance!
Lucas

@greg7mdp
Copy link
Owner

The user of the class could do this for example:

template <typename K, typename V>
using parallel_flat_hash_map = phmap::parallel_flat_hash_map<K, V, phmap::priv::hash_default_hash<V>, phmap::priv::hash_default_eq<K>, phmap::priv::Allocator<phmap::priv::Pair<K, V>>, std::mutex>;

and then use parallel_flat_hash_map as the template argument for MyClass

@lczech
Copy link
Author

lczech commented Oct 23, 2024

Thanks! It didn't work with exactly that, as the N template param seems to be needed (and it isn't part of your suggested typedef), but as I am happy with the defaults for most template params anyway, I got it to work with this:

template <typename K, typename V>
using parallel_flat_hash_map = phmap::parallel_flat_hash_map<K, V>;

and plug that one into MyClass.

Cheers and thanks again!
Lucas

@lczech lczech closed this as completed Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants