From 47f3737fb4debec248626e1a6001d91a1fde79d5 Mon Sep 17 00:00:00 2001 From: Ferdinand Bachmann Date: Sat, 18 Apr 2020 13:46:17 +0200 Subject: [PATCH] fix ring-buffer for simple constexpr usage --- include/ring-buffer.h | 2 ++ test/push-iterate-const-constexpr.cpp | 9 ++++++--- test/push-iterate-constexpr.cpp | 9 ++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/include/ring-buffer.h b/include/ring-buffer.h index 939681f..6cf6d70 100644 --- a/include/ring-buffer.h +++ b/include/ring-buffer.h @@ -2,6 +2,7 @@ #define _RING_BUFFER_H #include +#include #include #include @@ -20,6 +21,7 @@ private: public: basic_ring_buffer() = default; ~basic_ring_buffer() = default; + CONSTEXPR basic_ring_buffer(const Container& other) COND_NOEXCEPT(noexcept(Container(other))) : container(other) {} basic_ring_buffer(const basic_ring_buffer& other) = default; basic_ring_buffer(basic_ring_buffer&& other) = default; basic_ring_buffer& operator=(const basic_ring_buffer& other) = default; diff --git a/test/push-iterate-const-constexpr.cpp b/test/push-iterate-const-constexpr.cpp index 2deb805..93080ac 100644 --- a/test/push-iterate-const-constexpr.cpp +++ b/test/push-iterate-const-constexpr.cpp @@ -1,6 +1,7 @@ #include #include +#ifdef RING_BUFFER_CONSTEXPR template constexpr bool array_equal(const std::array& a, const std::array& b) { for (size_t i = 0; i < N; i++) { @@ -11,7 +12,8 @@ constexpr bool array_equal(const std::array& a, const std::array& b) } constexpr bool test() { - ring_buffer buf; + std::array init{0, 0, 0, 0}; + ring_buffer buf(init); const ring_buffer& const_buf = buf; buf.push_back(1); @@ -23,7 +25,7 @@ constexpr bool test() { { std::array expected{3, 4, 5, 6}; - std::array actual; + std::array actual{0, 0, 0, 0}; int actual_index = 0; for (const int& i : const_buf) { actual[actual_index++] = i; @@ -35,7 +37,7 @@ constexpr bool test() { { std::array expected{4, 5, 6, 7}; - std::array actual; + std::array actual{0, 0, 0, 0}; int actual_index = 0; for (const int& i : const_buf) { actual[actual_index++] = i; @@ -47,5 +49,6 @@ constexpr bool test() { } static_assert(test()); +#endif int main() {} diff --git a/test/push-iterate-constexpr.cpp b/test/push-iterate-constexpr.cpp index 16e1ccc..226bf5a 100644 --- a/test/push-iterate-constexpr.cpp +++ b/test/push-iterate-constexpr.cpp @@ -1,6 +1,7 @@ #include #include +#ifdef RING_BUFFER_CONSTEXPR template constexpr bool array_equal(const std::array& a, const std::array& b) { for (size_t i = 0; i < N; i++) { @@ -11,7 +12,8 @@ constexpr bool array_equal(const std::array& a, const std::array& b) } constexpr bool test() { - ring_buffer buf; + std::array init{0, 0, 0, 0}; + ring_buffer buf(init); buf.push_back(1); buf.push_back(2); @@ -22,7 +24,7 @@ constexpr bool test() { { std::array expected{13, 14, 15, 16}; - std::array actual; + std::array actual{0, 0, 0, 0}; int actual_index = 0; for (int& i : buf) { i += 10; @@ -35,7 +37,7 @@ constexpr bool test() { { std::array expected{24, 25, 26, 17}; - std::array actual; + std::array actual{0, 0, 0, 0}; int actual_index = 0; for (int& i : buf) { i += 10; @@ -48,5 +50,6 @@ constexpr bool test() { } static_assert(test()); +#endif int main() {}