Skip to content

Commit 874ded3

Browse files
authored
refactor(util): migrate util/* SFINAE patterns to C++20 concepts (#7632)
1 parent 3049880 commit 874ded3

4 files changed

Lines changed: 21 additions & 27 deletions

File tree

include/util/alias.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ template <typename ToNumeric, typename FromAlias> inline ToNumeric from_alias(co
142142
return {static_cast<ToNumeric>(static_cast<const FromAlias::value_type>(from))};
143143
}
144144

145-
template <typename ToAlias,
146-
typename FromNumeric,
147-
typename = std::enable_if_t<!std::is_same<ToAlias, FromNumeric>::value>>
145+
template <typename ToAlias, typename FromNumeric>
146+
requires(!std::is_same_v<ToAlias, FromNumeric>)
148147
inline ToAlias to_alias(const FromNumeric &from)
149148
{
150149
static_assert(std::is_arithmetic<FromNumeric>::value, "Numeric needs to be an arithmetic type");

include/util/filtered_integer_range.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#ifndef FILTERED_INTEGER_RANGE_HPP
22
#define FILTERED_INTEGER_RANGE_HPP
33

4+
#include <concepts>
45
#include <cstddef>
56
#include <iterator>
6-
#include <type_traits>
77

88
namespace osrm::util
99
{
@@ -80,11 +80,9 @@ template <typename Integer, typename Filter> class filtered_range
8080
};
8181

8282
template <typename Integer, typename Filter>
83-
filtered_range<Integer, Filter> filtered_irange(
84-
const Integer first,
85-
const Integer last,
86-
const Filter &filter,
87-
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr) noexcept
83+
filtered_range<Integer, Filter>
84+
filtered_irange(const Integer first, const Integer last, const Filter &filter) noexcept
85+
requires std::integral<Integer>
8886
{
8987
return filtered_range<Integer, Filter>(first, last, filter);
9088
}

include/util/indexed_data.hpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <boost/assert.hpp>
1212

1313
#include <array>
14+
#include <concepts>
1415
#include <functional>
1516
#include <iterator>
1617
#include <limits>
@@ -359,24 +360,20 @@ template <typename GroupBlockPolicy, storage::Ownership Ownership> struct Indexe
359360
const IndexedDataImpl &index_data);
360361

361362
private:
362-
template <typename Iter, typename T>
363-
using IsValueIterator = std::enable_if_t<
364-
std::is_same<T,
365-
std::remove_const_t<typename std::iterator_traits<Iter>::value_type>>::value>;
366-
367-
template <typename T = ResultType, typename Iter, typename = IsValueIterator<Iter, ValueType>>
368-
std::enable_if<!std::is_same<T, std::string_view>::value, T>::type adapt(const Iter first,
369-
const Iter last) const
363+
template <typename T = ResultType, typename Iter>
364+
requires std::same_as<std::remove_const_t<typename std::iterator_traits<Iter>::value_type>,
365+
ValueType>
366+
T adapt(const Iter first, const Iter last) const
370367
{
371-
return ResultType(first, last);
372-
}
373-
374-
template <typename T = ResultType, typename Iter, typename = IsValueIterator<Iter, ValueType>>
375-
std::enable_if<std::is_same<T, std::string_view>::value, T>::type adapt(const Iter first,
376-
const Iter last) const
377-
{
378-
auto diff = std::distance(first, last);
379-
return diff == 0 ? ResultType() : ResultType(&*first, diff);
368+
if constexpr (std::is_same_v<T, std::string_view>)
369+
{
370+
auto diff = std::distance(first, last);
371+
return diff == 0 ? ResultType() : ResultType(&*first, diff);
372+
}
373+
else
374+
{
375+
return ResultType(first, last);
376+
}
380377
}
381378

382379
template <typename T> using Vector = util::ViewOrVector<T, Ownership>;

include/util/static_rtree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,9 @@ class StaticRTree
454454
/**
455455
* Constructs an empty RTree for de-serialization.
456456
*/
457-
template <typename = std::enable_if<Ownership == storage::Ownership::Container>>
458457
explicit StaticRTree(const std::filesystem::path &on_disk_file_name,
459458
const Vector<Coordinate> &coordinate_list)
459+
requires(Ownership == storage::Ownership::Container)
460460
: m_coordinate_list(coordinate_list.data(), coordinate_list.size()),
461461
m_objects(mmapFile<EdgeDataT>(on_disk_file_name, m_objects_region))
462462
{

0 commit comments

Comments
 (0)