-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_traits.cpp
More file actions
35 lines (25 loc) · 938 Bytes
/
type_traits.cpp
File metadata and controls
35 lines (25 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <type_traits>
#include <concepts>
// https://stackoverflow.com/questions/62644070/differences-between-stdis-convertible-and-stdconvertible-to-in-practice
// https://en.cppreference.com/w/cpp/concepts/convertible_to
struct From;
struct To {
explicit To(From) = delete;
};
struct From {
operator To();
};
// std::is_convertible<From, To> (the type trait) checks is type From is implicitly convertible to type To.
// std::convertible_to<From, To> (the concept) checks that From is **both** implicitly and **explicitly** convertible to To.
static_assert(std::is_convertible_v<From, To>);
static_assert(!std::convertible_to<From, To>);
template<typename T, typename U>
struct is_explicity_convertible
{
static constexpr bool value = std::is_constructible_v<T, U> && !std::is_constructible_v<U, T>;
};
// this example is wrong supposedly.
static_assert(!is_explicity_convertible<int, long>::value);
int main()
{
}