From 44d3021717975fd0673be115ac488496e2ffad1d Mon Sep 17 00:00:00 2001 From: edward Date: Fri, 7 Nov 2025 21:08:56 +0000 Subject: [PATCH] WIP: error: use of parameter from containing function --- CMakeLists.txt | 21 ++++++ CMakePresets.json | 41 +++++++++++ cmake-build.sh | 4 ++ cmake-test.sh | 3 + p0059r0.pdf => doc/p0059r0.pdf | Bin p0059r1.pdf => doc/p0059r1.pdf | Bin examples/CMakeLists.txt | 10 +++ .../ring_view.test.cc | 8 ++- fixed_ring.test.cc | 23 ------ src/CMakeLists.txt | 13 ++++ fixed_ring.h => src/fixed_ring.h | 16 +++-- heap_span.h => src/heap_span.h | 5 +- ring_span.h => src/ring_span.h | 5 +- test/CMakeLists.txt | 68 ++++++++++++++++++ test/compiles_at_all.cc | 7 +- test/fixed_ring.test.cc | 45 ++++++++++++ test/run-all-tests.sh | 5 -- vcpkg-configuration.json | 14 ++++ vcpkg.json | 5 ++ 19 files changed, 253 insertions(+), 40 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 CMakePresets.json create mode 100755 cmake-build.sh create mode 100755 cmake-test.sh rename p0059r0.pdf => doc/p0059r0.pdf (100%) rename p0059r1.pdf => doc/p0059r1.pdf (100%) create mode 100644 examples/CMakeLists.txt rename ring_view.test.cc => examples/ring_view.test.cc (83%) delete mode 100644 fixed_ring.test.cc create mode 100644 src/CMakeLists.txt rename fixed_ring.h => src/fixed_ring.h (93%) rename heap_span.h => src/heap_span.h (99%) rename ring_span.h => src/ring_span.h (99%) create mode 100644 test/CMakeLists.txt create mode 100644 test/fixed_ring.test.cc delete mode 100755 test/run-all-tests.sh create mode 100644 vcpkg-configuration.json create mode 100644 vcpkg.json diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..5fc427a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.5) + +project(ring-view) + +enable_testing() + +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) # Ensures no compiler-specific extensions are used +set(CMAKE_CXX_FLAGS_DEBUG_INIT "-Wall -Wextra -pedantic -Werror") +set(CMAKE_CXX_FLAGS_RELEASE_INIT "-Wall -Wextra -pedantic -Werror") + +set(CMAKE_CXX_FLAGS_DEBUG "-g") +set(CMAKE_CXX_FLAGS_RELEASE "-O3") + +# Catch2 +find_package(Catch2 CONFIG REQUIRED) + +add_subdirectory(src) +add_subdirectory(test) +add_subdirectory(examples) diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..c983e9b --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,41 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "debug", + "displayName": "Linux Debug", + "description": "debug build for Linux", + "generator": "Unix Makefiles", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "cacheVariables": { + "CMAKE_CXX_STANDARD": "23", + "CMAKE_BUILD_TYPE": "Debug", + "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}", + "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" + } + }, + { + "name": "release", + "displayName": "Linux Release", + "description": "release build for Linux", + "generator": "Unix Makefiles", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "cacheVariables": { + "CMAKE_CXX_STANDARD": "23", + "CMAKE_BUILD_TYPE": "Release", + "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}", + "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" + } + } + ], + "buildPresets": [ + { + "name": "debug", + "configurePreset": "debug" + }, + { + "name": "release", + "configurePreset": "release" + } + ] +} \ No newline at end of file diff --git a/cmake-build.sh b/cmake-build.sh new file mode 100755 index 0000000..52c3f0a --- /dev/null +++ b/cmake-build.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +cmake --preset=release -DCMAKE_CXX_COMPILER=g++-14 +cmake --build --preset=release -j$(nproc) \ No newline at end of file diff --git a/cmake-test.sh b/cmake-test.sh new file mode 100755 index 0000000..96b8416 --- /dev/null +++ b/cmake-test.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +ctest --test-dir out/build/release --output-on-failure $* diff --git a/p0059r0.pdf b/doc/p0059r0.pdf similarity index 100% rename from p0059r0.pdf rename to doc/p0059r0.pdf diff --git a/p0059r1.pdf b/doc/p0059r1.pdf similarity index 100% rename from p0059r1.pdf rename to doc/p0059r1.pdf diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..e936426 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,10 @@ +add_executable( + ring_view_test_executable + ring_view.test.cc +) + +target_link_libraries( + ring_view_test_executable + PRIVATE + ring_view +) \ No newline at end of file diff --git a/ring_view.test.cc b/examples/ring_view.test.cc similarity index 83% rename from ring_view.test.cc rename to examples/ring_view.test.cc index 0c421a3..e8b7b78 100644 --- a/ring_view.test.cc +++ b/examples/ring_view.test.cc @@ -25,7 +25,11 @@ int main() std::queue q(rv); q.push(nullptr); q.push(nullptr); - q.front(); - q.back(); + { + const auto _ = std::move(q.front()); + } + { + const auto _ = std::move(q.back()); + } q.pop(); } diff --git a/fixed_ring.test.cc b/fixed_ring.test.cc deleted file mode 100644 index a369382..0000000 --- a/fixed_ring.test.cc +++ /dev/null @@ -1,23 +0,0 @@ -#include "fixed_ring.h" -#include -#include - -int main() -{ - fixed_ring fr; - assert(fr.size() == 0); - fr.push(1); - assert(fr.size() == 1); assert(fr.front() == 1); assert(fr.back() == 1); - fr.push(2); - assert(fr.size() == 2); assert(fr.front() == 1); assert(fr.back() == 2); - fr.push(3); - assert(fr.size() == 3); assert(fr.front() == 1); assert(fr.back() == 3); - fr.push(4); - assert(fr.size() == 4); assert(fr.front() == 1); assert(fr.back() == 4); - fr.push(5); - assert(fr.size() == 4); assert(fr.front() == 2); assert(fr.back() == 5); - fr.push(6); - assert(fr.size() == 4); assert(fr.front() == 3); assert(fr.back() == 6); - fr.pop(); - assert(fr.size() == 3); assert(fr.front() == 4); assert(fr.back() == 6); -} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..21f902f --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,13 @@ +add_library( + ring_view + INTERFACE + ring_span.h + heap_span.h + fixed_ring.h +) + +target_include_directories( + ring_view + INTERFACE + ${CMAKE_CURRENT_SOURCE_DIR} +) \ No newline at end of file diff --git a/fixed_ring.h b/src/fixed_ring.h similarity index 93% rename from fixed_ring.h rename to src/fixed_ring.h index 3966d7c..75d6807 100644 --- a/fixed_ring.h +++ b/src/fixed_ring.h @@ -1,6 +1,7 @@ -#pragma once +#ifndef FIXED_RING_H +#define FIXED_RING_H -#include "ring_view.h" +#include "ring_span.h" #include #include @@ -11,6 +12,9 @@ template + using ring_span = std::experimental::ring_span; + using container_type = Container; using value_type = typename Container::value_type; using size_type = typename Container::size_type; @@ -57,7 +61,7 @@ class fixed_ring { ctr_ = rhs.ctr_; auto first_idx = (&*rhs.rv_.begin() - rhs.ctr_.begin()); - rv_ = ring_view(ctr_.begin(), ctr_.end(), ctr_.begin() + first_idx, rhs.rv_.size()); + rv_ = ring_span(ctr_.begin(), ctr_.end(), ctr_.begin() + first_idx, rhs.rv_.size()); return *this; } @@ -65,7 +69,7 @@ class fixed_ring { auto first_idx = (&*rhs.rv_.begin() - rhs.ctr_.begin()); ctr_ = std::move(rhs.ctr_); - rv_ = ring_view(ctr_.begin(), ctr_.end(), ctr_.begin() + first_idx, rhs.rv_.size()); + rv_ = ring_span(ctr_.begin(), ctr_.end(), ctr_.begin() + first_idx, rhs.rv_.size()); return *this; } @@ -111,5 +115,7 @@ class fixed_ring {} Container ctr_; - ring_view rv_; + ring_span rv_; }; + +#endif \ No newline at end of file diff --git a/heap_span.h b/src/heap_span.h similarity index 99% rename from heap_span.h rename to src/heap_span.h index 26c4277..d459017 100644 --- a/heap_span.h +++ b/src/heap_span.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef HEAP_SPAN_H +#define HEAP_SPAN_H #include #include @@ -187,3 +188,5 @@ class heap_span }; } } // namespace std::experimental + +#endif \ No newline at end of file diff --git a/ring_span.h b/src/ring_span.h similarity index 99% rename from ring_span.h rename to src/ring_span.h index b4bef48..ef55426 100644 --- a/ring_span.h +++ b/src/ring_span.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef RING_SPAN_H +#define RING_SPAN_H // Reference implementation of P0059R1 + errata. @@ -271,3 +272,5 @@ class ring_iterator } // namespace detail } } // namespace std::experimental + +#endif \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..9b32d25 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,68 @@ +add_executable( + compiles_at_all + compiles_at_all.cc +) + +target_link_libraries( + compiles_at_all + PRIVATE + Catch2::Catch2 + Catch2::Catch2WithMain + ring_view +) + +add_executable( + fixed_ring_test + fixed_ring.test.cc +) + +target_link_libraries( + fixed_ring_test + PRIVATE + Catch2::Catch2 + Catch2::Catch2WithMain + ring_view +) + +add_executable( + is_default_constructible + is_default_constructible.cc +) + +target_link_libraries( + is_default_constructible + PRIVATE + Catch2::Catch2 + Catch2::Catch2WithMain + ring_view +) + +add_executable( + moveonly_type + moveonly_type.cc +) + +target_link_libraries( + moveonly_type + PRIVATE + Catch2::Catch2 + Catch2::Catch2WithMain + ring_view +) + +add_executable( + p0059r1 + p0059r1.cc +) + +target_link_libraries( + p0059r1 + PRIVATE + Catch2::Catch2 + Catch2::Catch2WithMain + ring_view +) + + +include(Catch) +catch_discover_tests(compiles_at_all) \ No newline at end of file diff --git a/test/compiles_at_all.cc b/test/compiles_at_all.cc index d3cd3fe..d7170c2 100644 --- a/test/compiles_at_all.cc +++ b/test/compiles_at_all.cc @@ -1,6 +1,7 @@ +#include #include "ring_span.h" -int main() -{ -} +TEST_CASE("compiles-at-all") { + REQUIRE(1 == 1); +} \ No newline at end of file diff --git a/test/fixed_ring.test.cc b/test/fixed_ring.test.cc new file mode 100644 index 0000000..2d74878 --- /dev/null +++ b/test/fixed_ring.test.cc @@ -0,0 +1,45 @@ +#include + +#include "fixed_ring.h" +#include + +TEST_CASE("fixed-ring_test") { + + fixed_ring fr; + REQUIRE(fr.size() == 0); + + fr.push(1); + REQUIRE(fr.size() == 1); + REQUIRE(fr.front() == 1); + REQUIRE(fr.back() == 1); + + fr.push(2); + REQUIRE(fr.size() == 2); + REQUIRE(fr.front() == 1); + REQUIRE(fr.back() == 2); + + fr.push(3); + REQUIRE(fr.size() == 3); + REQUIRE(fr.front() == 1); + REQUIRE(fr.back() == 3); + + fr.push(4); + REQUIRE(fr.size() == 4); + REQUIRE(fr.front() == 1); + REQUIRE(fr.back() == 4); + + fr.push(5); + REQUIRE(fr.size() == 4); + REQUIRE(fr.front() == 2); + REQUIRE(fr.back() == 5); + + fr.push(6); + REQUIRE(fr.size() == 4); + REQUIRE(fr.front() == 3); + REQUIRE(fr.back() == 6); + + fr.pop(); + REQUIRE(fr.size() == 3); + REQUIRE(fr.front() == 4); + REQUIRE(fr.back() == 6); +} diff --git a/test/run-all-tests.sh b/test/run-all-tests.sh deleted file mode 100755 index ab62f55..0000000 --- a/test/run-all-tests.sh +++ /dev/null @@ -1,5 +0,0 @@ -for i in ./*.cc; do - echo $i - g++ -std=c++1y -O3 -I .. $i -o ./a.out - ./a.out -done diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json new file mode 100644 index 0000000..f14bf6b --- /dev/null +++ b/vcpkg-configuration.json @@ -0,0 +1,14 @@ +{ + "default-registry": { + "kind": "git", + "baseline": "b1b19307e2d2ec1eefbdb7ea069de7d4bcd31f01", + "repository": "https://github.com/microsoft/vcpkg" + }, + "registries": [ + { + "kind": "artifact", + "location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip", + "name": "microsoft" + } + ] +} diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 0000000..ff20cfc --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,5 @@ +{ + "dependencies": [ + "catch2" + ] +}