Replace template conditionals with C++20 requires clause

Related: https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-constraints.html

PR #19424.
This commit is contained in:
Chocobo1 2023-08-09 20:33:19 +08:00 committed by GitHub
parent 33d767b765
commit 5c06d0aa75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 31 deletions

View file

@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Mike Tzou (Chocobo1)
* Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
@ -28,25 +29,16 @@
#pragma once
#include <type_traits>
namespace Algorithm
{
template <typename T, typename = void>
struct HasMappedType
: std::false_type
{
};
template <typename T>
struct HasMappedType<T, std::void_t<typename T::mapped_type>>
: std::true_type
concept HasMappedType = requires
{
typename T::mapped_type;
};
// To be used with associative array types, such as QMap, QHash and its variants
template <typename T, typename BinaryPredicate
, typename std::enable_if_t<HasMappedType<T>::value, int> = 0>
template <HasMappedType T, typename BinaryPredicate>
void removeIf(T &dict, BinaryPredicate &&p)
{
auto it = dict.begin();

View file

@ -29,9 +29,9 @@
#pragma once
#include <concepts>
#include <functional>
#include <set>
#include <type_traits>
#include "algorithm.h"
@ -70,8 +70,8 @@ public:
return BaseType::empty();
}
template <typename std::enable_if_t<std::is_same_v<value_type, QString>, int> = 0>
QString join(const QString &separator) const
requires std::same_as<value_type, QString>
{
auto iter = BaseType::cbegin();
if (iter == BaseType::cend())

View file

@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Mike Tzou (Chocobo1)
* Copyright (C) 2016 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2014 sledgehammer999 <hammered999@gmail.com>
*
@ -41,10 +42,7 @@
#include "utils/string.h"
template <typename T>
struct IsQFlags : std::false_type {};
template <typename T>
struct IsQFlags<QFlags<T>> : std::true_type {};
concept IsQFlags = std::same_as<T, QFlags<typename T::enum_type>>;
// There are 2 ways for class `T` provide serialization support into `SettingsStorage`:
// 1. If the `T` state is intended for users to edit (via a text editor), then
@ -76,7 +74,7 @@ public:
const auto value = loadValue<QString>(key);
return Utils::String::toEnum(value, defaultValue);
}
else if constexpr (IsQFlags<T>::value)
else if constexpr (IsQFlags<T>)
{
const typename T::Int value = loadValue(key, static_cast<typename T::Int>(defaultValue));
return T {value};
@ -101,7 +99,7 @@ public:
storeValueImpl(key, value.toString());
else if constexpr (std::is_enum_v<T>)
storeValueImpl(key, Utils::String::fromEnum(value));
else if constexpr (IsQFlags<T>::value)
else if constexpr (IsQFlags<T>)
storeValueImpl(key, static_cast<typename T::Int>(value));
else
storeValueImpl(key, QVariant::fromValue(value));

View file

@ -67,8 +67,9 @@ namespace Utils::String
QString fromDouble(double n, int precision);
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
template <typename T>
QString fromEnum(const T &value)
requires std::is_enum_v<T>
{
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
"Enumeration underlying type has to be int.");
@ -77,8 +78,9 @@ namespace Utils::String
return QString::fromLatin1(metaEnum.valueToKey(static_cast<int>(value)));
}
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
template <typename T>
T toEnum(const QString &serializedValue, const T &defaultValue)
requires std::is_enum_v<T>
{
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
"Enumeration underlying type has to be int.");

View file

@ -55,9 +55,9 @@ namespace Utils
constexpr Version() = default;
template <typename ... Ts
, typename std::enable_if_t<std::conjunction_v<std::is_convertible<Ts, int>...>, int> = 0>
template <typename ... Ts>
constexpr Version(Ts ... params)
requires std::conjunction_v<std::is_convertible<Ts, int>...>
: m_components {{params ...}}
{
static_assert((sizeof...(Ts) <= N), "Too many parameters provided");

View file

@ -47,14 +47,14 @@ public:
private slots:
void testHasMappedType() const
{
QVERIFY(static_cast<bool>(Algorithm::HasMappedType<std::map<bool, bool>>::value));
QVERIFY(static_cast<bool>(Algorithm::HasMappedType<std::unordered_map<bool, bool>>::value));
QVERIFY(static_cast<bool>(Algorithm::HasMappedType<QHash<bool, bool>>::value));
QVERIFY(static_cast<bool>(Algorithm::HasMappedType<QMap<bool, bool>>::value));
static_assert(Algorithm::HasMappedType<std::map<bool, bool>>);
static_assert(Algorithm::HasMappedType<std::unordered_map<bool, bool>>);
static_assert(Algorithm::HasMappedType<QHash<bool, bool>>);
static_assert(Algorithm::HasMappedType<QMap<bool, bool>>);
QVERIFY(!static_cast<bool>(Algorithm::HasMappedType<std::set<bool>>::value));
QVERIFY(!static_cast<bool>(Algorithm::HasMappedType<std::unordered_set<bool>>::value));
QVERIFY(!static_cast<bool>(Algorithm::HasMappedType<QSet<bool>>::value));
static_assert(!Algorithm::HasMappedType<std::set<bool>>);
static_assert(!Algorithm::HasMappedType<std::unordered_set<bool>>);
static_assert(!Algorithm::HasMappedType<QSet<bool>>);
}
void testMappedTypeRemoveIf() const