Skip to content

What about a constructor from an existing std::array of key value pairs? #14

Description

@gotnone

What do you think about adding an additional constructor for the case where one already has the data array created?

diff --git a/include/constexpr_hash_map/constexpr_hash_map.hpp b/include/constexpr_hash_map/constexpr_hash_map.hpp
index c1b9d50..68d740b 100644
--- a/include/constexpr_hash_map/constexpr_hash_map.hpp
+++ b/include/constexpr_hash_map/constexpr_hash_map.hpp
@@ -42,6 +42,15 @@ public:
         static_assert(N == sizeof...(elements), "Elements size doesn't match expected size of a hash-map");
     }
 
+    /// @brief The only other construction that might be used, array must be provided in the constructor.
+    /// @param std::array<std::pair<K,V>, N>, cannot be empty
+    explicit constexpr hash_map(data_type arr) noexcept
+    : data{std::move(arr)}
+    {
+        static_assert(N > 0, "N should be positive");
+        static_assert(N == data.size(), "Array size doesn't match expected size of a hash-map");
+    }
+
     /// @brief Searches map for a given key and returns iterator.
     /// @param key key to be searched for
     /// @return constant iterator to an element (cend, if not found)

Someone with more experience template programming may find a way to deduce the N, K and V parameters from the std::array itself.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions