C++ Library Extensions 2022.12.09
To help learn modern C++ programming
053-decay.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
3template<typename Type>
4using flat_type_t1 = std::remove_cv_t<std::remove_reference_t<std::decay_t<Type>>>;
5
6template<typename Type>
7using flat_type_t2 = std::remove_cv_t<std::decay_t<std::remove_reference_t<Type>>>;
8
11
13{
14 const int array[]{10};
15
16 using type_of_array = decltype(array);
17 using ref_type_of_array = decltype((array));
18
19 stream << Tpf_GetTypeName( type_of_array) << endl;
20 stream << Tpf_GetTypeName( ref_type_of_array) << endl;
25}
26
27int sum(int, float) { return 0; }
28
29template<typename Type>
30decltype(auto) decay(Type&& arg)
31{
32 using type = std::remove_reference_t<Type>;
33
34 if constexpr(std::is_array_v<type> || std::is_function_v<type>)
35 return std::decay_t<type>(std::forward<Type>(arg));
36 else
37 return std::forward<Type>(arg);
38}
39
41{
42 const int const_array[]{1, 2, 3, 4};
43
44 int array[] {1, 2, 3};
45
46 decay(const_array);
47 std::cout << "\n---------" << std::endl;
48 decay(array);
49
50 std::cout << "\n============" << std::endl;
51
52 decay(std::move(const_array));
53 std::cout << "\n---------" << std::endl;
54 decay(std::move(array));
55
56}
57
59{
60 decay(sum);
61
62}
63int main()
64{
65 // test_flat_type_t();
66 // test_decay();
67
69}
auto & cout
tpf::sstream stream
Definition: 053-decay.cpp:9
std::remove_cv_t< std::remove_reference_t< std::decay_t< Type > > > flat_type_t1
Definition: 053-decay.cpp:4
auto endl
Definition: 053-decay.cpp:10
std::remove_cv_t< std::decay_t< std::remove_reference_t< Type > > > flat_type_t2
Definition: 053-decay.cpp:7
void test_decay_func()
Definition: 053-decay.cpp:58
void test_decay()
Definition: 053-decay.cpp:40
int main()
Definition: 053-decay.cpp:63
decltype(auto) decay(Type &&arg)
Definition: 053-decay.cpp:30
void test_flat_type_t()
Definition: 053-decay.cpp:12
int sum(int, float)
Definition: 053-decay.cpp:27
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.
#define Tpf_GetTypeName(type_arg)
A macro that returns type_arg's string name.
Definition: tpf_types.hpp:1422