From 313abea5dd169d0f46e022767628376c0d516012 Mon Sep 17 00:00:00 2001 From: Tessil Date: Sun, 15 Sep 2024 18:24:43 +0100 Subject: [PATCH] Move to C++17 dropping C++11 support --- README.md | 2 +- doxygen.conf | 2 +- include/tsl/robin_hash.h | 64 +++------------------------------------- tests/CMakeLists.txt | 4 +-- 4 files changed, 8 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index f012a2b..09fc544 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ If the project has been installed through `make install`, you can also use `find The library is available in [vcpkg](https://github.com/Microsoft/vcpkg/tree/master/ports/robin-map) and [conan](https://conan.io/center/tsl-robin-map). It's also present in [Debian](https://packages.debian.org/buster/robin-map-dev), [Ubuntu](https://packages.ubuntu.com/disco/robin-map-dev) and [Fedora](https://apps.fedoraproject.org/packages/robin-map-devel) package repositories. -The code should work with any C++11 standard-compliant compiler and has been tested with GCC 4.8.4, Clang 3.5.0 and Visual Studio 2015. +The code should work with any C++17 standard-compliant compiler. To run the tests you will need the Boost Test library and CMake. diff --git a/doxygen.conf b/doxygen.conf index 276a2a6..534fdb5 100644 --- a/doxygen.conf +++ b/doxygen.conf @@ -1081,7 +1081,7 @@ CLANG_ASSISTED_PARSING = YES # specified with INPUT and INCLUDE_PATH. # This tag requires that the tag CLANG_ASSISTED_PARSING is set to YES. -CLANG_OPTIONS = -std=c++11 +CLANG_OPTIONS = -std=c++17 #--------------------------------------------------------------------------- # Configuration options related to the alphabetical class index diff --git a/include/tsl/robin_hash.h b/include/tsl/robin_hash.h index 03d23a9..78043e4 100644 --- a/include/tsl/robin_hash.h +++ b/include/tsl/robin_hash.h @@ -66,12 +66,6 @@ template struct is_power_of_two_policy> : std::true_type {}; -// Only available in C++17, we need to be compatible with C++11 -template -const T& clamp(const T& v, const T& lo, const T& hi) { - return std::min(hi, std::max(lo, v)); -} - template static T numeric_cast(U value, const char* error_message = "numeric_cast() failed.") { @@ -254,22 +248,14 @@ class bucket_entry : public bucket_entry_hash { value_type& value() noexcept { tsl_rh_assert(!empty()); -#if defined(__cplusplus) && __cplusplus >= 201703L return *std::launder( reinterpret_cast(std::addressof(m_value))); -#else - return *reinterpret_cast(std::addressof(m_value)); -#endif } const value_type& value() const noexcept { tsl_rh_assert(!empty()); -#if defined(__cplusplus) && __cplusplus >= 201703L return *std::launder( reinterpret_cast(std::addressof(m_value))); -#else - return *reinterpret_cast(std::addressof(m_value)); -#endif } distance_type dist_from_ideal_bucket() const noexcept { @@ -541,7 +527,6 @@ class robin_hash : private Hash, private KeyEqual, private GrowthPolicy { }; public: -#if defined(__cplusplus) && __cplusplus >= 201402L robin_hash(size_type bucket_count, const Hash& hash, const KeyEqual& equal, const Allocator& alloc, float min_load_factor = DEFAULT_MIN_LOAD_FACTOR, @@ -569,47 +554,6 @@ class robin_hash : private Hash, private KeyEqual, private GrowthPolicy { this->min_load_factor(min_load_factor); this->max_load_factor(max_load_factor); } -#else - /** - * C++11 doesn't support the creation of a std::vector with a custom allocator - * and 'count' default-inserted elements. The needed contructor `explicit - * vector(size_type count, const Allocator& alloc = Allocator());` is only - * available in C++14 and later. We thus must resize after using the - * `vector(const Allocator& alloc)` constructor. - * - * We can't use `vector(size_type count, const T& value, const Allocator& - * alloc)` as it requires the value T to be copyable. - */ - robin_hash(size_type bucket_count, const Hash& hash, const KeyEqual& equal, - const Allocator& alloc, - float min_load_factor = DEFAULT_MIN_LOAD_FACTOR, - float max_load_factor = DEFAULT_MAX_LOAD_FACTOR) - : Hash(hash), - KeyEqual(equal), - GrowthPolicy(bucket_count), - m_buckets_data(alloc), - m_buckets(static_empty_bucket_ptr()), - m_bucket_count(bucket_count), - m_nb_elements(0), - m_grow_on_next_insert(false), - m_try_shrink_on_next_insert(false) { - if (bucket_count > max_bucket_count()) { - TSL_RH_THROW_OR_TERMINATE(std::length_error, - "The map exceeds its maximum bucket count."); - } - - if (m_bucket_count > 0) { - m_buckets_data.resize(m_bucket_count); - m_buckets = m_buckets_data.data(); - - tsl_rh_assert(!m_buckets_data.empty()); - m_buckets_data.back().set_as_last_bucket(); - } - - this->min_load_factor(min_load_factor); - this->max_load_factor(max_load_factor); - } -#endif robin_hash(const robin_hash& other) : Hash(other), @@ -1073,13 +1017,13 @@ class robin_hash : private Hash, private KeyEqual, private GrowthPolicy { float max_load_factor() const { return m_max_load_factor; } void min_load_factor(float ml) { - m_min_load_factor = clamp(ml, float(MINIMUM_MIN_LOAD_FACTOR), - float(MAXIMUM_MIN_LOAD_FACTOR)); + m_min_load_factor = std::clamp(ml, float(MINIMUM_MIN_LOAD_FACTOR), + float(MAXIMUM_MIN_LOAD_FACTOR)); } void max_load_factor(float ml) { - m_max_load_factor = clamp(ml, float(MINIMUM_MAX_LOAD_FACTOR), - float(MAXIMUM_MAX_LOAD_FACTOR)); + m_max_load_factor = std::clamp(ml, float(MINIMUM_MAX_LOAD_FACTOR), + float(MAXIMUM_MAX_LOAD_FACTOR)); m_load_threshold = size_type(float(bucket_count()) * m_max_load_factor); tsl_rh_assert(bucket_count() == 0 || m_load_threshold < bucket_count()); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index af9ad7d..6d3bbcd 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -8,7 +8,7 @@ add_executable(tsl_robin_map_tests "main.cpp" "robin_map_tests.cpp" "robin_set_tests.cpp") -target_compile_features(tsl_robin_map_tests PRIVATE cxx_std_11) +target_compile_features(tsl_robin_map_tests PRIVATE cxx_std_17) if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") target_compile_options(tsl_robin_map_tests PRIVATE -Werror -Wall -Wextra -Wold-style-cast -DTSL_DEBUG -UNDEBUG) @@ -18,7 +18,7 @@ endif() # Boost::unit_test_framework set(Boost_USE_STATIC_LIBS ON) -find_package(Boost 1.54.0 REQUIRED COMPONENTS unit_test_framework) +find_package(Boost REQUIRED COMPONENTS unit_test_framework) target_link_libraries(tsl_robin_map_tests PRIVATE Boost::unit_test_framework) # tsl::robin_map