diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f238c5..7bb0056 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,16 +2,11 @@ cmake_minimum_required(VERSION 3.12) project(cpp_zip) set(PROJECT_VERSION 0.1.0) -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED YES) -set(CXX_EXTENSIONS NO) - list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") include(warnings) -include_directories(include) - add_library(msd_zip INTERFACE) +target_compile_features(msd_zip INTERFACE cxx_std_17) target_include_directories(msd_zip INTERFACE include) option(ENABLE_TESTS "Build tests" OFF) @@ -23,3 +18,5 @@ if (ENABLE_TESTS) enable_testing() add_subdirectory(tests) endif() + +install(DIRECTORY include/ DESTINATION include) diff --git a/include/msd/zip.hpp b/include/msd/zip.hpp index 35569a5..7696c83 100644 --- a/include/msd/zip.hpp +++ b/include/msd/zip.hpp @@ -92,7 +92,7 @@ class zip_iterator { * @param offset The number of positions to advance. * @return A new `zip_iterator` advanced by the specified offset. */ - zip_iterator operator+(const std::size_t offset) const + zip_iterator operator+(const difference_type offset) const { auto iterator = *this; iterator.advance(std::index_sequence_for{}, offset); @@ -130,7 +130,7 @@ class zip_iterator { * @param offset The number of positions to move back. * @return A new `zip_iterator` moved back by the specified offset. */ - zip_iterator operator-(const int offset) const + zip_iterator operator-(const difference_type offset) const { auto iterator = *this; iterator.advance(std::index_sequence_for{}, -offset); @@ -187,7 +187,7 @@ class zip_iterator { * @param offset The number of positions to advance. */ template - void advance(std::index_sequence, const int offset) + void advance(std::index_sequence, const difference_type offset) { ((std::advance(std::get(iterators_), offset)), ...); } @@ -326,7 +326,7 @@ class zip { value_type back() { assert(!empty()); // LCOV_EXCL_LINE - return *std::prev(begin() + size()); + return *std::prev(begin() + static_cast(size())); } /** @@ -338,7 +338,7 @@ class zip { value_type back() const { assert(!empty()); // LCOV_EXCL_LINE - return *std::prev(begin() + size()); + return *std::prev(begin() + static_cast(size())); } /** @@ -351,7 +351,7 @@ class zip { value_type operator[](const std::size_t offset) const { assert(offset < size()); - return *std::next(begin(), offset); + return *std::next(begin(), static_cast(offset)); } private: @@ -380,7 +380,23 @@ class zip { template Iterator end_impl(std::index_sequence) const { - return std::next(Iterator{std::get(containers_).begin()...}, size()); + const auto zip_size = size(); + return Iterator{advance_iterator(std::get(containers_).begin(), zip_size)...}; + } + + /** + * @brief Advances an iterator by a non-negative offset. + * + * @tparam Iterator Type of the iterator to advance. + * @param iterator The iterator to advance. + * @param offset The number of positions to advance. + * @return The advanced iterator. + */ + template + static Iterator advance_iterator(Iterator iterator, const std::size_t offset) + { + std::advance(iterator, static_cast::difference_type>(offset)); + return iterator; } /** @@ -393,7 +409,8 @@ class zip { template std::size_t size_impl(std::index_sequence) const { - return std::min({std::distance(std::get(containers_).begin(), std::get(containers_).end())...}); + return static_cast( + std::min({std::distance(std::get(containers_).begin(), std::get(containers_).end())...})); } /** diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b34b60a..7c2e049 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,9 +19,14 @@ endif () # Test macro function(package_add_test TESTNAME) add_executable(${TESTNAME} ${ARGN}) + set_target_properties(${TESTNAME} PROPERTIES + CXX_STANDARD 17 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO + ) set_target_warnings(${TESTNAME} PRIVATE) - target_link_libraries(${TESTNAME} gtest gtest_main gmock) + target_link_libraries(${TESTNAME} msd_zip gtest gtest_main gmock) if (ENABLE_COVERAGE) target_link_libraries(${TESTNAME} -lgcov -lubsan) @@ -67,6 +72,11 @@ if (ENABLE_BENCHMARKS) endif () add_executable(zip_benchmark zip_benchmark.cpp) - target_link_libraries(zip_benchmark benchmark) + set_target_properties(zip_benchmark PROPERTIES + CXX_STANDARD 17 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO + ) + target_link_libraries(zip_benchmark msd_zip benchmark) set_target_warnings(zip_benchmark PRIVATE) endif ()