tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
common_type.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_COMMON_TYPE_HPP
5#define TETL_TYPE_TRAITS_COMMON_TYPE_HPP
6
7#include <etl/_type_traits/decay.hpp>
8#include <etl/_type_traits/declval.hpp>
9#include <etl/_type_traits/void_t.hpp>
10
11namespace etl {
12
13/// \brief Determines the common type among all types `T...`, that is the type
14/// all `T...` can be implicitly converted to. If such a type exists, the member
15/// type names that type. Otherwise, there is no member type.
16///
17/// https://en.cppreference.com/w/cpp/types/common_type
18template <typename... T>
19struct common_type;
20
21template <typename T>
22struct common_type<T> : common_type<T, T> { };
23
24namespace detail {
25template <typename T1, typename T2>
26using cond_t = decltype(false ? declval<T1>() : declval<T2>());
27
28template <typename T1, typename T2, typename = void>
29struct common_type_2_impl { };
30
31template <typename T1, typename T2>
32struct common_type_2_impl<T1, T2, void_t<cond_t<T1, T2>>> {
33 using type = decay_t<cond_t<T1, T2>>;
34};
35
36template <typename AlwaysVoid, typename T1, typename T2, typename... R>
37struct common_type_multi_impl { };
38
39template <typename T1, typename T2, typename... R>
40struct common_type_multi_impl<void_t<typename common_type<T1, T2>::type>, T1, T2, R...>
41 : common_type<typename common_type<T1, T2>::type, R...> { };
42} // namespace detail
43
44template <typename T1, typename T2>
45struct common_type<T1, T2> : detail::common_type_2_impl<decay_t<T1>, decay_t<T2>> { };
46
47template <typename T1, typename T2, typename... R>
48struct common_type<T1, T2, R...> : detail::common_type_multi_impl<void, T1, T2, R...> { };
49
50template <typename... T>
51using common_type_t = typename common_type<T...>::type;
52
53} // namespace etl
54
55#endif // TETL_TYPE_TRAITS_COMMON_TYPE_HPP
Definition adjacent_find.hpp:9