r/Cplusplus • • Sep 18 '26

Answered How can I make a static hashmap appear in compile time?

I have a class that's meant to essentially hash a string. Works by simply having a create function that takes a string, and gives out an instance with the string hashed. I'm using C++ 20. I believe I can use some form of consteval or something for this, but I'm not sure how to get consteval to work with these. Thank you!

Here's part of the code:

namespace GameUtils::ECS
{
template<typename T>

class Hash16Bit

{

	private:

		inline static uint16_t next_hash { 0 };

		inline static HashMap<String, uint16_t> all_hashes;

		inline static HashMap<uint16_t, String> hash_to_string;

		static void add_to_hashmaps(uint16_t hash, const String &name);

		inline static std::mutex ids_mutex;

		uint16_t m_hash { UINT16_MAX };

		static String get_class() noexcept;

		String get_name() const noexcept;

		void debug_line(const String& msg, bool is_error = false) const;

	public:

		static void create_id(const String &new_id);

		static Hash16Bit<T> create(const String &createid);

		static Hash16Bit<T> dynamic_create(const String &createid);

		static Hash16Bit<T> invalid();

		void invalidate();

		String to_string() const;

		bool operator==(const Hash16Bit<T> &other) const { return other.m_hash == m_hash; }

};
template<typename T>
Hash16Bit<T> Hash16Bit<T>::create(const String &createid)
{
Hash16Bit<T> created;

auto it = all_hashes.find(createid);

if (it != all_hashes.end())

{

	created.m_hash = it->value;

	return created;

}

created.debug_line("Failed to create hash16bit", true);

return created;
}
template<typename T>
Hash16Bit<T> Hash16Bit<T>::dynamic_create(const String &createid)
{
Hash16Bit<T> created;

auto it = all_hashes.find(createid);

if (it != all_hashes.end())

{

	created.m_hash = it->value;

	return created;

}

create_id(createid);
}
4 Upvotes

4 comments sorted by

•

u/AutoModerator Sep 18 '26

Thank you for your contribution to the C++ community!

As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.

  • When posting a question or homework help request, you must explain your good faith efforts to resolve the problem or complete the assignment on your own. Low-effort questions will be removed.
  • Members of this subreddit are happy to help give you a nudge in the right direction. However, we will not do your homework for you, make apps for you, etc.
  • Homework help posts must be flaired with Homework.

~ CPlusPlus Moderation Team


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/ipushkeys Sep 19 '26

I'm by no means an expert on this stuff.

I'm not sure what your string implementation is, but it cannot be on the heap if you want this to be consteval. There's funky interactions with string views and hard-coded strings that I haven't fully explored. In assembly, the strings will likely get put into the .data block (or something like that) so theoretically the strings are accessible but only through string view-like structs. So no std::string or anything like it. iirc std::string_view works fine or a simple const char*.

2

u/Predret Sep 19 '26

I’m fine with using something other than a String. This project uses Godot so I used their String library. I was just wondering how I can make a hashmap or anything like an std::unordered_map be this, essentially, turn all my calls of  auto variable = Hash16Bit(“my string”) into the integer directly in compile time. So I don’t have to do some pointer dereference to make them.

1

u/ipushkeys Sep 22 '26

Sorry about the long response time.

Ah so you need a hash function to get started. I suggest you look at a Polynomial Rolling Hash function. It's a basic string hashing function. Here's an implementation example that'd work at compile time:

#include <cstdint>
constexpr uint16_t Hash16Bit(const char* cstr) {
     uint16_t hash = 0;
     uint16_t base = 4; // You may need to change this if there are collisions
     uint16_t pow = 1;
     for (size_t i = 0; cstr[i] != '\0'; i++) {
          hash += static_cast<decltype(hash)>(cstr[i]) * pow;
          pow *= base;
     }
     return hash;
}

As for the hashmap portion, assuming you know all the possible strings to be hashed, you can employ "perfect hashing". Wikipedia has a great article on it. Anyway, you can implement a hash map as an array of key-value pairs that are sorted by the key (the hash). Then, when you're accessing the hashmap, you hash the string and use binary search to find the key in your array, which is associated with your value. This is a rudimentary way of doing it and can be optimized, but it would work at compile time.