Skip to content

Commit 82eea02

Browse files
Update the best_conversion_index_v (#182)
Update the docs and fix fragile tests that could break on some platforms. Should fix #161
1 parent 4c6e675 commit 82eea02

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

cetlvast/suites/unittest/test_type_traits_ext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ using cetl::type_traits_ext::partial;
8787
constexpr auto bad = std::numeric_limits<std::size_t>::max();
8888

8989
// Easy cases
90-
static_assert(best_conversion_index_v<universal_predicate, float, long, float, double, bool> == 1, "");
91-
static_assert(best_conversion_index_v<universal_predicate, float&, long, float, double, bool> == 1, "");
90+
static_assert(best_conversion_index_v<universal_predicate, float, long, float, bool> == 1, "");
91+
static_assert(best_conversion_index_v<universal_predicate, float&, long, float, bool> == 1, "");
9292
static_assert(best_conversion_index_v<universal_predicate, long, float> == 0, "");
9393
static_assert(best_conversion_index_v<universal_predicate, long&, float> == 0, "");
9494
static_assert(best_conversion_index_v<universal_predicate, int, float, int> == 1, "");

include/cetl/pf17/variant.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ constexpr std::size_t best_converting_ctor_index_v = type_traits_ext::
726726
best_conversion_index_v<type_traits_ext::partial<best_converting_ctor_predicate, U>::template type, U, Ts...>;
727727

728728
static_assert(best_converting_ctor_index_v<float, long, float, bool> == 1, "self-test failure");
729-
static_assert(best_converting_ctor_index_v<double, long, float, double, bool> == 2, "self-test failure");
729+
static_assert(best_converting_ctor_index_v<double, long, char, double, bool> == 2, "self-test failure");
730730

731731
// --------------------------------------------------------------------------------------------------------------------
732732

@@ -743,7 +743,7 @@ constexpr std::size_t best_converting_assignment_index_v = type_traits_ext::
743743
best_conversion_index_v<type_traits_ext::partial<best_converting_assignment_predicate, U>::template type, U, Ts...>;
744744

745745
static_assert(best_converting_assignment_index_v<float, long, float, bool> == 1, "self-test failure");
746-
static_assert(best_converting_assignment_index_v<double, long, float, double, bool> == 2, "self-test failure");
746+
static_assert(best_converting_assignment_index_v<double, long, char, double, bool> == 2, "self-test failure");
747747

748748
// --------------------------------------------------------------------------------------------------------------------
749749

include/cetl/type_traits_ext.hpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,12 @@ struct resolver<Q, 0, Ts...> : candidate<Q, 0, std::tuple_element_t<0, std::tupl
201201
template <template <typename...> class, typename, typename, typename = void>
202202
struct impl : std::integral_constant<std::size_t, std::numeric_limits<std::size_t>::max()>
203203
{};
204-
template <template <typename...> class Q, typename Fun, typename... Ts>
205-
struct impl<Q, Fun, types<Ts...>, void_t<decltype(resolver<Q, sizeof...(Ts) - 1U, Ts...>::match(std::declval<Fun>()))>>
206-
: decltype(resolver<Q, sizeof...(Ts) - 1U, Ts...>::match(std::declval<Fun>()))
204+
template <template <typename...> class Q, typename From, typename... Ts>
205+
struct impl<Q,
206+
From,
207+
types<Ts...>,
208+
void_t<decltype(resolver<Q, sizeof...(Ts) - 1U, Ts...>::match(std::declval<From>()))>>
209+
: decltype(resolver<Q, sizeof...(Ts) - 1U, Ts...>::match(std::declval<From>()))
207210
{
208211
static_assert(sizeof...(Ts) > 0, "tried to match conversion to no types.");
209212
};
@@ -222,15 +225,17 @@ struct impl<Q, Fun, types<Ts...>, void_t<decltype(resolver<Q, sizeof...(Ts) - 1U
222225
///
223226
/// Hint: to weed out narrowing conversions, use \ref is_convertible_without_narrowing in the predicate.
224227
///
225-
/// If no suitable conversion is available, the value is `std::numeric_limits<std::size_t>::max()`.
228+
/// If no suitable conversion is available, or the best conversion is ambiguous,
229+
/// the value is `std::numeric_limits<std::size_t>::max()`.
230+
/// One way to get this error is to choose between `long` and `int` on a platform where they have the same size.
226231
///
227232
/// \code
228233
/// best_conversion_index_v<universal_predicate, long, float> == 0
229-
/// best_conversion_index_v<universal_predicate, float, long, float, double, bool> == 1
234+
/// best_conversion_index_v<universal_predicate, float, long, float, bool> == 1
230235
/// best_conversion_index_v<universal_predicate, int, long, float, bool> == ambiguity
231236
///
232-
/// best_conversion_index_v<std::is_signed, long, char, long, unsigned long> == 1
233-
/// best_conversion_index_v<std::is_unsigned, long, char, long, unsigned long> == 2
237+
/// best_conversion_index_v<std::is_signed, long, unsigned char, long, unsigned long> == 1
238+
/// best_conversion_index_v<std::is_unsigned, long, signed char, long, unsigned long> == 2
234239
/// best_conversion_index_v<std::is_volatile, char, int, const int, volatile int> == 2
235240
///
236241
/// best_conversion_index_v<partial<is_convertible_without_narrowing, int>::template type, int, float, bool, long> == 2

0 commit comments

Comments
 (0)