Skip to content

Commit 23c8b20

Browse files
committed
migrate to C++20
1 parent f9515ee commit 23c8b20

25 files changed

Lines changed: 112 additions & 167 deletions

BUILD.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This guide describes how to build **MMapper** from source on Linux, macOS, and W
88

99
Ensure the following tools are installed:
1010

11-
- A C++17-compatible compiler (e.g., GCC, Clang, MSVC)
11+
- A C++20-compatible compiler (e.g., GCC 11+, Clang 16+, MSVC 19.29+)
1212
- [CMake 3.20+](https://cmake.org/)
1313
- [Ninja](https://ninja-build.org/)
1414
- [Qt 6.4+](https://www.qt.io/) (6.8.3 recommended) with `QtWebSockets` and `QtMultimedia`

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ project(mmapper CXX)
33

44
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
55

6-
set(CMAKE_CXX_STANDARD 17)
6+
set(CMAKE_CXX_STANDARD 20)
77
set(CMAKE_CXX_STANDARD_REQUIRED ON)
88
set(CMAKE_CXX_EXTENSIONS OFF)
99

@@ -419,6 +419,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
419419
endif()
420420

421421
add_compile_options(-Weverything)
422+
add_compile_options(-Wno-c++20-compat)
422423
add_compile_options(-Wno-c++98-c++11-compat-binary-literal) # never useful.
423424
add_compile_options(-Wno-c++98-compat) # never useful.
424425
add_compile_options(-Wno-c++98-compat-pedantic) # sometimes useful.
@@ -461,7 +462,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
461462
add_compile_options(-Werror=shorten-64-to-32)
462463
add_compile_options(-Werror=sign-conversion) # can warn about hard-to-spot bugs.
463464
add_compile_options(-Werror=switch)
464-
add_compile_options(-Werror=unused-result) # required for c++17 [[nodiscard]]
465+
add_compile_options(-Werror=unused-result) # required for [[nodiscard]]
465466
add_compile_options(-Werror=weak-vtables) # can result in crashes for ODR violations.
466467

467468
# remove noise (most of these cannot be fixed)
@@ -472,6 +473,7 @@ if(MSVC)
472473
add_definitions(-DNOMINMAX)
473474
# modernize the __cplusplus value
474475
add_compile_options(/Zc:__cplusplus)
476+
add_compile_options(/Zc:preprocessor)
475477
add_compile_options(/utf-8)
476478
add_compile_options(/permissive-)
477479
if(CMAKE_BUILD_TYPE_UPPER MATCHES "^RELWITHDEBINFO$")

src/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ endif()
771771

772772
set_target_properties(
773773
mmapper PROPERTIES
774-
CXX_STANDARD 17
774+
CXX_STANDARD 20
775775
CXX_STANDARD_REQUIRED ON
776776
CXX_EXTENSIONS OFF
777777
COMPILE_FLAGS "${WARNING_FLAGS}"
@@ -781,15 +781,15 @@ set_target_properties(
781781

782782
set_target_properties(
783783
mm_global PROPERTIES
784-
CXX_STANDARD 17
784+
CXX_STANDARD 20
785785
CXX_STANDARD_REQUIRED ON
786786
CXX_EXTENSIONS OFF
787787
COMPILE_FLAGS "${WARNING_FLAGS}"
788788
)
789789

790790
set_target_properties(
791791
mm_map PROPERTIES
792-
CXX_STANDARD 17
792+
CXX_STANDARD 20
793793
CXX_STANDARD_REQUIRED ON
794794
CXX_EXTENSIONS OFF
795795
COMPILE_FLAGS "${WARNING_FLAGS}"

src/display/Textures.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,8 @@ namespace mctp {
232232
namespace detail {
233233
// converts from EnumIndexedArray<SharedMMTexture, ...> to EnumIndexedArray<MMTexArrayPosition, ...>
234234
template<typename T>
235-
auto typeHack(const T &)
236-
-> std::enable_if_t<std::is_same_v<typename T::value_type, SharedMMTexture>,
237-
EnumIndexedArray<MMTexArrayPosition, typename T::index_type, T::SIZE>>;
235+
requires(std::is_same_v<typename T::value_type, SharedMMTexture>)
236+
auto typeHack(const T &) -> EnumIndexedArray<MMTexArrayPosition, typename T::index_type, T::SIZE>;
238237

239238
template<typename T>
240239
struct NODISCARD Proxy

src/global/AnsiOstream.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,17 @@ class NODISCARD AnsiOstream final
135135
void write(char16_t codepoint);
136136
void write(char32_t codepoint);
137137
void write(std::string_view sv);
138+
void write(std::u8string_view sv)
139+
{
140+
write(std::string_view{reinterpret_cast<const char *>(sv.data()), sv.size()});
141+
}
138142

139143
template<typename T>
140-
auto write(const T n)
141-
-> std::enable_if_t<(std::is_integral_v<T> && !std::is_same_v<char32_t, std::decay_t<T>>
142-
&& sizeof(char) < sizeof(T))
143-
|| std::is_floating_point_v<T>,
144-
void>
144+
requires((std::is_integral_v<T> and sizeof(T) > sizeof(char)
145+
and not(std::is_same_v<char16_t, std::decay_t<T>>
146+
or std::is_same_v<char32_t, std::decay_t<T>>))
147+
or std::is_floating_point_v<T>)
148+
void write(const T n)
145149
{
146150
auto s = std::to_string(n);
147151
write(std::string_view{s});

src/global/Array.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@ class NODISCARD Array : public std::array<T, N>
3535

3636
public:
3737
template<typename First, typename... Types>
38+
requires(std::is_same_v<First, Types> && ...)
3839
explicit constexpr Array(First &&first, Types &&...args)
3940
: std::array<T, N>{std::forward<First>(first), std::forward<Types>(args)...}
4041
{
4142
static_assert(N == 1 + sizeof...(Types), "missing initializers");
4243
}
4344
};
4445

45-
#if __cpp_deduction_guides >= 201606
46-
// deducation guide copied from std::array
4746
template<typename T, typename... U>
48-
Array(T, U...) -> Array<std::enable_if_t<(std::is_same_v<T, U> && ...), T>, 1 + sizeof...(U)>;
49-
#endif
47+
requires(std::is_same_v<T, U> && ...)
48+
Array(T, U...) -> Array<T, 1 + sizeof...(U)>;
5049
} // namespace MMapper

src/global/Charset-Utf8.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,7 @@ NODISCARD static constexpr bool is7bit(const char c) noexcept
552552
}
553553
NODISCARD static constexpr bool is7bit(const std::string_view sv) noexcept
554554
{
555-
// NOLINTNEXTLINE (can't use std::all_of() in constexpr in c++17)
556-
for (const char c : sv) {
557-
if (!is7bit(c)) {
558-
return false;
559-
}
560-
}
561-
return true;
555+
return std::all_of(sv.begin(), sv.end(), [](char c) { return is7bit(c); });
562556
}
563557

564558
NODISCARD static constexpr Utf8ValidationEnum validateUtf8(std::string_view sv) noexcept

src/global/Charset.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ ALLOW_DISCARD QString &toAsciiInPlace(QString &str)
348348

349349
// NOTE: 128 (0x80) was not converted to 'z' before.
350350
for (QChar &qc : str) {
351-
// c++17 if statement with initializer
352351
if (const char ch = mmqt::toLatin1(qc); !isAscii(ch)) {
353352
qc = QLatin1Char{charset::conversion::latin1ToAscii(ch)};
354353
}

src/global/ConfigConsts-Computed.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "ConfigEnums.h"
66

7+
#include <functional>
78
#include <stdexcept>
89
#include <string_view>
910

@@ -44,7 +45,7 @@ static inline constexpr PackageEnum CURRENT_PACKAGE = [] {
4445
throw std::runtime_error("unsupported package type");
4546
}();
4647

47-
static inline constexpr const PlatformEnum CURRENT_PLATFORM = [] {
48+
static inline constexpr PlatformEnum CURRENT_PLATFORM = std::invoke([]() constexpr -> PlatformEnum {
4849
#if defined(Q_OS_WIN)
4950
return PlatformEnum::Windows;
5051
#elif defined(Q_OS_MAC)
@@ -56,14 +57,15 @@ static inline constexpr const PlatformEnum CURRENT_PLATFORM = [] {
5657
#else
5758
throw std::runtime_error("unsupported platform");
5859
#endif
59-
}();
60+
});
6061

61-
static inline constexpr const EnvironmentEnum CURRENT_ENVIRONMENT = [] {
62+
static inline constexpr EnvironmentEnum CURRENT_ENVIRONMENT = std::invoke(
63+
[]() constexpr -> EnvironmentEnum {
6264
#if Q_PROCESSOR_WORDSIZE == 4
63-
return EnvironmentEnum::Env32Bit;
65+
return EnvironmentEnum::Env32Bit;
6466
#elif Q_PROCESSOR_WORDSIZE == 8
65-
return EnvironmentEnum::Env64Bit;
67+
return EnvironmentEnum::Env64Bit;
6668
#else
67-
throw std::runtime_error("unsupported environment");
69+
throw std::runtime_error("unsupported environment");
6870
#endif
69-
}();
71+
});

src/global/MakeQPointer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
namespace mmqt {
1414
template<typename T, typename... Args>
15-
NODISCARD auto makeQPointer(Args &&...args)
16-
-> std::enable_if_t<std::is_base_of_v<QObject, T>, QPointer<T>>
15+
requires(std::is_base_of_v<QObject, T>)
16+
NODISCARD QPointer<T> makeQPointer(Args &&...args)
1717
{
1818
auto ptr = std::make_unique<T>(std::forward<Args>(args)...);
1919
if (ptr->QObject::parent() == nullptr) {

0 commit comments

Comments
 (0)