Skip to content

Commit 20032da

Browse files
committed
Fix: set CMAKE_CXX_STANDARD=20 only if not defined
Prevent static local in constructor to compile with C++17 too Prevent use of spaceship operator if not available NOTE: This project needs at least C++20!
1 parent 9d239b2 commit 20032da

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

.github/workflows/ci_tests.yml

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,14 @@ jobs:
4949
}
5050
]
5151
},
52-
{ "cxxversions": ["c++23", "c++20", "c++17"],
52+
{ "cxxversions": ["c++23", "c++20"],
5353
"tests": [{ "stdlibs": ["libstdc++"], "tests": ["Release.Default"]}]
5454
}
5555
]
5656
},
5757
{ "versions": ["14", "13"],
5858
"tests": [
59-
{ "cxxversions": ["c++26", "c++23", "c++20", "c++17"],
60-
"tests": [{ "stdlibs": ["libstdc++"], "tests": ["Release.Default"]}]
61-
}
62-
]
63-
},
64-
{
65-
"versions": ["12", "11"],
66-
"tests": [
67-
{ "cxxversions": ["c++23", "c++20", "c++17"],
59+
{ "cxxversions": ["c++26", "c++23", "c++20"],
6860
"tests": [{ "stdlibs": ["libstdc++"], "tests": ["Release.Default"]}]
6961
}
7062
]
@@ -83,7 +75,7 @@ jobs:
8375
}
8476
]
8577
},
86-
{ "cxxversions": ["c++23", "c++20", "c++17"],
78+
{ "cxxversions": ["c++23", "c++20"],
8779
"tests": [
8880
{"stdlibs": ["libstdc++", "libc++"], "tests": ["Release.Default"]}
8981
]
@@ -92,7 +84,7 @@ jobs:
9284
},
9385
{ "versions": ["20", "19", "18"],
9486
"tests": [
95-
{ "cxxversions": ["c++26", "c++23", "c++20", "c++17"],
87+
{ "cxxversions": ["c++26", "c++23", "c++20"],
9688
"tests": [
9789
{"stdlibs": ["libstdc++", "libc++"], "tests": ["Release.Default"]}
9890
]
@@ -101,10 +93,10 @@ jobs:
10193
},
10294
{ "versions": ["17"],
10395
"tests": [
104-
{ "cxxversions": ["c++26", "c++23", "c++20", "c++17"],
96+
{ "cxxversions": ["c++26", "c++23", "c++20"],
10597
"tests": [{"stdlibs": ["libc++"], "tests": ["Release.Default"]}]
10698
},
107-
{ "cxxversions": ["c++20", "c++17"],
99+
{ "cxxversions": ["c++20"],
108100
"tests": [{"stdlibs": ["libstdc++"], "tests": ["Release.Default"]}]
109101
}
110102
]
@@ -113,7 +105,7 @@ jobs:
113105
"appleclang": [
114106
{ "versions": ["latest"],
115107
"tests": [
116-
{ "cxxversions": ["c++26", "c++23", "c++20", "c++17"],
108+
{ "cxxversions": ["c++26", "c++23", "c++20"],
117109
"tests": [{ "stdlibs": ["libc++"], "tests": ["Release.Default"]}]
118110
}
119111
]

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22

3-
cmake_minimum_required(VERSION 3.25)
3+
cmake_minimum_required(VERSION 3.25...4.2)
44

5-
set(CMAKE_CXX_STANDARD 23)
5+
if(NOT DEFINED CMAKE_CXX_STANDARD)
6+
set(CMAKE_CXX_STANDARD 20) # NEEDED! for std::type_identity, std::format, std::char8_t, ...!
7+
endif()
68

79
project(
810
beman.cstring_view # CMake Project Name, which is also the name of the top-level

CMakePresets.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"generator": "Ninja",
88
"binaryDir": "${sourceDir}/build/${presetName}",
99
"cacheVariables": {
10-
"CMAKE_CXX_STANDARD": "20",
1110
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
1211
"CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "./infra/cmake/use-fetch-content.cmake"
1312
}

examples/example.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using namespace std::literals;
88
using namespace beman::literals;
99

10+
#if __cpp_impl_three_way_comparison >= 201907L
1011
std::string_view to_string(std::strong_ordering order) {
1112
if (order == std::strong_ordering::equal) {
1213
return "equal";
@@ -21,6 +22,7 @@ std::string_view to_string(std::strong_ordering order) {
2122
return "internal error";
2223
}
2324
}
25+
#endif
2426

2527
int main() {
2628
std::string s = "hello world";
@@ -39,7 +41,9 @@ int main() {
3941
std::cout << ("hello"_csv != "goodbye"sv) << "\n";
4042
std::cout << ("hello"_csv != "goodbye"_csv) << "\n";
4143
std::cout << (z0 == z1) << "\n";
44+
#if __cpp_impl_three_way_comparison >= 201907L
4245
std::cout << to_string(z0 <=> z1) << "\n";
46+
#endif
4347
std::cout << z0[z0.size()] * 1 << "\n";
4448
std::cout << z0.c_str() << "\n";
4549
std::cout << "\"" << empty << "\"\n";
@@ -61,7 +65,9 @@ int main() {
6165
std::cout << (L"hello"_csv != L"goodbye"sv) << "\n";
6266
std::cout << (L"hello"_csv != L"goodbye"_csv) << "\n";
6367
std::cout << (wz0 == wz1) << "\n";
68+
#if __cpp_impl_three_way_comparison >= 201907L
6469
std::cout << to_string(wz0 <=> wz1) << "\n";
70+
#endif
6571
std::cout << wz0[wz0.size()] * 1 << "\n";
6672
std::wcout << std::format(L"{}\n", wz0.c_str());
6773
std::wcout << std::format(L"\"{}\"\n", wempty);

include/beman/cstring_view/cstring_view.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <format>
88
#include <ranges>
99
#include <stdexcept>
10+
#include <type_traits> // for std::type_identity_t
1011
#include <string_view>
1112
#include <string>
1213

@@ -31,9 +32,13 @@ constexpr bool operator==(basic_cstring_view<charT, traits>
3132
std::type_identity_t<basic_cstring_view<charT, traits>> y) noexcept;
3233

3334
template <class charT, class traits>
35+
36+
#if __cpp_impl_three_way_comparison >= 201907L
3437
constexpr auto operator<=>(basic_cstring_view<charT, traits> x,
3538
std::type_identity_t<basic_cstring_view<charT, traits>> y) noexcept;
3639

40+
#endif
41+
3742
// [cstring.view.io], inserters and extractors
3843
template <class charT, class traits>
3944
std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os,
@@ -110,7 +115,7 @@ class basic_cstring_view {
110115

111116
// [cstring.view.cons], construction and assignment
112117
constexpr basic_cstring_view() noexcept : size_() {
113-
static const charT empty_string[1]{};
118+
// XXX static const charT empty_string[1]{};
114119
data_ = std::data(empty_string);
115120
}
116121
constexpr basic_cstring_view(const basic_cstring_view&) noexcept = default;
@@ -313,6 +318,8 @@ class basic_cstring_view {
313318
private:
314319
const_pointer data_; // exposition only
315320
size_type size_; // exposition only
321+
322+
static constexpr charT empty_string[1]{}; // NOLINT
316323
};
317324

318325
inline namespace literals {

0 commit comments

Comments
 (0)