You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
This version fixes an issue in the static assert and provides a deduction guide.
diff --git a/include/constexpr_hash_map/constexpr_hash_map.hpp b/include/constexpr_hash_map/constexpr_hash_map.hpp
index c1b9d50..2a01401 100644
--- a/include/constexpr_hash_map/constexpr_hash_map.hpp
+++ b/include/constexpr_hash_map/constexpr_hash_map.hpp
@@ -42,6 +42,14 @@ 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");
+ }
+
/// @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)
@@ -170,6 +178,9 @@ protected:
private:
data_type data;
};
+
+template <std::size_t N, typename K, typename V>
+hash_map(std::array<std::pair<K, V>, N>) -> hash_map<N, K, V>;
} // namespace burda::ct
#endif // CONSTEXPR_HASH_MAP_CONSTEXPR_HASH_MAP_HPP
What do you think about adding an additional constructor for the case where one already has the data array created?
Someone with more experience template programming may find a way to deduce the N, K and V parameters from the std::array itself.
The text was updated successfully, but these errors were encountered: