tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
is_convertible.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_TYPE_TRAITS_IS_CONVERTIBLE_HPP
5#define TETL_TYPE_TRAITS_IS_CONVERTIBLE_HPP
6
7#include <etl/_type_traits/bool_constant.hpp>
8#include <etl/_type_traits/declval.hpp>
9#include <etl/_type_traits/is_void.hpp>
10
11namespace etl {
12
13namespace detail {
14template <typename>
15using true_type_for = etl::true_type;
16
17template <typename T>
18auto test_returnable(int) -> true_type_for<T()>;
19template <typename>
20auto test_returnable(...) -> etl::false_type;
21
22template <typename From, typename To>
23auto test_nonvoid_convertible(int) -> true_type_for<decltype(etl::declval<void (&)(To)>()(etl::declval<From>()))>;
24template <typename, typename>
25auto test_nonvoid_convertible(...) -> etl::false_type;
26
27} // namespace detail
28
29/// \brief If the imaginary function definition `To test() { return
30/// etl::declval<From>(); }` is well-formed, (that is, either
31/// `etl::declval<From>()` can be converted to To using implicit conversions, or
32/// both From and To are possibly cv-qualified void), provides the member
33/// constant value equal to true. Otherwise value is false. For the purposes of
34/// this check, the use of `etl::declval` in the return statement is not
35/// considered an odr-use. Access checks are performed as if from a context
36/// unrelated to either type. Only the validity of the immediate context of the
37/// expression in the return statement (including conversions to the return
38/// type) is considered.
39template <typename From, typename To>
41 : bool_constant<
42 (decltype(detail::test_returnable<To>(0))::value
43 && decltype(detail::test_nonvoid_convertible<From, To>(0))::value)
44 || (is_void_v<From> && is_void_v<To>)
45 > { };
46
47template <typename From, typename To>
48inline constexpr bool is_convertible_v = is_convertible<From, To>::value;
49
50} // namespace etl
51
52#endif // TETL_TYPE_TRAITS_IS_CONVERTIBLE_HPP
Definition adjacent_find.hpp:9
constexpr bool is_convertible_v
Definition is_convertible.hpp:48
If the imaginary function definition To test() { return etl::declval<From>(); } is well-formed,...
Definition is_convertible.hpp:45