From 9cf44150e537e5d200dde3688286d7a494e4cda3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rimas=20Misevi=C4=8Dius?= Date: Sat, 4 Nov 2023 20:51:50 +0200 Subject: [PATCH] Add `constexpr` to some `str_view` class member functions (#23) * add UPA_CONSTEXPR_14 macro * add workflow to compile with g++ using C++14 --- .github/workflows/test-ubuntu.yml | 5 +++++ include/upa/config.h | 6 +++++- include/upa/str_view.h | 32 +++++++++++++++++-------------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test-ubuntu.yml b/.github/workflows/test-ubuntu.yml index 8f004420..bfdbd363 100644 --- a/.github/workflows/test-ubuntu.yml +++ b/.github/workflows/test-ubuntu.yml @@ -53,6 +53,11 @@ jobs: cmake_options: "-DURL_AMALGAMATED=ON -DURL_BUILD_EXAMPLES=ON" before_cmake: tools/amalgamate.sh + - name: g++-9 C++14 + cxx_compiler: g++-9 + cxx_standard: 14 + cmake_options: "-DURL_BUILD_EXAMPLES=ON" + - name: g++-9 C++11 cxx_compiler: g++-9 cxx_standard: 11 diff --git a/include/upa/config.h b/include/upa/config.h index 3fa19a61..f225287a 100644 --- a/include/upa/config.h +++ b/include/upa/config.h @@ -31,8 +31,12 @@ #endif // Define UPA_CPP_14 if compiler supports C++14 or later -#if defined(_MSVC_LANG) ? (_MSVC_LANG >= 201402) : (__cplusplus >= 201402) +// Note: Visual Studio 2015 (14.0; _MSC_VER == 1900) lacks sufficient C++14 support +#if defined(_MSVC_LANG) ? (_MSVC_LANG >= 201402 && _MSC_VER > 1900) : (__cplusplus >= 201402) # define UPA_CPP_14 +# define UPA_CONSTEXPR_14 constexpr +#else +# define UPA_CONSTEXPR_14 inline #endif #endif // UPA_CONFIG_H diff --git a/include/upa/str_view.h b/include/upa/str_view.h index 4a3cae77..0281df5a 100644 --- a/include/upa/str_view.h +++ b/include/upa/str_view.h @@ -6,12 +6,16 @@ #ifndef UPA_STR_VIEW_H #define UPA_STR_VIEW_H +#include "config.h" #include #include #include namespace upa { +/// @brief A very simplified implementation of the basic_string_view +/// +/// It is used when compiling with C++11 or C++14 compilers. template> class str_view { public: @@ -31,41 +35,41 @@ class str_view { // static constexpr size_type npos = size_type(-1); // constructors - str_view() noexcept = default; - str_view(const str_view&) noexcept = default; - str_view(const CharT* ptr, size_type len) : ptr_(ptr), len_(len) {} + constexpr str_view() noexcept = default; + constexpr str_view(const str_view&) noexcept = default; + constexpr str_view(const CharT* ptr, size_type len) : ptr_(ptr), len_(len) {} str_view(const CharT* ptr) : ptr_(ptr), len_(Traits::length(ptr)) {} // assignment - str_view& operator=(const str_view&) noexcept = default; + UPA_CONSTEXPR_14 str_view& operator=(const str_view&) noexcept = default; // destructor ~str_view() noexcept = default; // iterator support - const_iterator begin() const noexcept { return ptr_; } - const_iterator end() const noexcept { return ptr_ + len_; } + constexpr const_iterator begin() const noexcept { return ptr_; } + constexpr const_iterator end() const noexcept { return ptr_ + len_; } // capacity - size_type size() const noexcept { return len_; } - size_type length() const noexcept { return len_; } - bool empty() const noexcept { return len_ == 0; } + constexpr size_type size() const noexcept { return len_; } + constexpr size_type length() const noexcept { return len_; } + constexpr bool empty() const noexcept { return len_ == 0; } // element access - const_reference operator[](size_type ind) const { + constexpr const_reference operator[](size_type ind) const { return ptr_[ind]; } - const_pointer data() const noexcept { return ptr_; } + constexpr const_pointer data() const noexcept { return ptr_; } // modifiers - void remove_prefix(size_type n) { + UPA_CONSTEXPR_14 void remove_prefix(size_type n) { ptr_ += n; len_ -= n; } - void remove_suffix(size_type n) { + UPA_CONSTEXPR_14 void remove_suffix(size_type n) { len_ -= n; } - void swap(str_view& x) noexcept { + UPA_CONSTEXPR_14 void swap(str_view& x) noexcept { const str_view tmp{x}; x = *this; *this = tmp;