kblib 0.2.3
General utilities library for modern C++
fakestd.cpp
Go to the documentation of this file.
1#include "kblib/fakestd.h"
2#include "catch.hpp"
3
4template <class... Args>
5struct Ref {
6
7 auto operator->() -> auto {
8 return kblib::meta_type_t<Args...>{};
9 } // present only if sizeof...(Args) == 1
10};
11
12// static decltype(&Ref<int>::operator->) f;
13// static decltype(&Ref<int, int>::operator->) f2;
14
15static Ref<int> r;
17
18KBLIB_UNUSED static auto blah() -> void {
19 r.operator->();
20 // r2.operator->();
21 std::tuple<int, int> a{0, 0}, b{0, 1};
22 kblib::swap(a, b);
23}
24
25TEST_CASE("to_unique(lambda)") {
26 // to_unique can make unique_ptrs to anonymous types
27 auto p = kblib::to_unique(new auto([] {}), [](auto p) { delete p; });
28}
29
30TEST_CASE("signed_cast") {
31 REQUIRE(kblib::signed_cast<unsigned>(-1ll) == -1ull);
32 REQUIRE(kblib::signed_cast<signed>(static_cast<unsigned short>(78)) == 78);
33 static_assert(
34 std::is_same<signed short, decltype(kblib::signed_cast<signed>(
35 std::declval<unsigned short>()))>::value,
36 "");
37 static_assert(std::is_same<unsigned short,
38 decltype(kblib::signed_cast<unsigned>(
39 std::declval<unsigned short>()))>::value,
40 "");
41 static_assert(std::is_same<signed short, decltype(kblib::signed_cast<signed>(
42 std::declval<short>()))>::value,
43 "");
44 static_assert(std::is_same<unsigned short,
45 decltype(kblib::signed_cast<unsigned>(
46 std::declval<unsigned short>()))>::value,
47 "");
48 static_assert(
49 std::is_same<signed int, decltype(kblib::signed_cast<signed>(
50 std::declval<unsigned int>()))>::value,
51 "");
52 // auto _1 = kblib::signed_cast<signed>(0.0);
53 // auto _2 = kblib::signed_cast<double>(0);
54}
55
56#if KBLIB_USE_CXX17
57template <class T>
58KBLIB_UNUSED constexpr auto type_name_f() -> std::string_view {
59 using namespace std;
60# ifdef __clang__
61 string_view p = __PRETTY_FUNCTION__;
62 return string_view(p.data() + 36, p.size() - 36 - 1);
63# elif defined(__GNUC__)
64 string_view p = __PRETTY_FUNCTION__;
65# if __cplusplus < 201402
66 return string_view(p.data() + 36, p.size() - 36 - 1);
67# else
68 return string_view(p.data() + 49, p.find(';', 49) - 49);
69# endif
70# elif defined(_MSC_VER)
71 string_view p = __FUNCSIG__;
72 return string_view(p.data() + 84, p.size() - 84 - 7);
73# endif
74}
75#endif
76
77#if KBLIB_USE_CXX17
78TEST_CASE("next_larger") {
79 static_assert(
80 std::is_same<kblib::detail::next_larger_signed<signed char>::type,
81 short>::value,
82 "signed char < short");
83 static_assert(
84 std::is_same<kblib::detail::next_larger_signed<short>::type, int>::value,
85 "short < int");
86 static_assert(
87 std::is_same<kblib::detail::next_larger_signed<int>::type, long>::value,
88 "int < long");
89# if 0
90 std::cout
91 << "Next_larger: \n"
92 << "signed char: "
93 << type_name_f<kblib::detail::next_larger_signed<signed char>::type>()
94 << '\n'
95 << "short: "
97 << "int: "
98 << type_name_f<kblib::detail::next_larger_signed<int>::type>() << '\n';
99 // long long can't be promoted
100 // std::cout
101 // << "long long: "
102 // << type_name_f<kblib::detail::next_larger_signed<long long>::type>()
103 // << '\n';
104# endif
105}
106#endif
TEST_CASE("to_unique(lambda)")
Definition: fakestd.cpp:25
KBLIB_UNUSED constexpr auto type_name_f() -> std::string_view
Definition: fakestd.cpp:58
This header provides some features of C++17 <type_traits> and other headers for C++14,...
constexpr auto a(const std::initializer_list< T > &a) -> auto
Index an array literal without naming its type.
Definition: simple.h:255
struct kblib::@0 swap
auto to_unique(owner< T * > p) -> std::unique_ptr< T >
Definition: fakestd.h:580
typename meta_type< Ts... >::type meta_type_t
Definition: fakestd.h:512
Definition: bits.h:721
Definition: fakestd.cpp:5
auto operator->() -> auto
Definition: fakestd.cpp:7
typename decltype(ints_map::template get_default< max_val< T >+1 >())::type type
Definition: fakestd.h:815
#define KBLIB_UNUSED
This internal macro is used to provide a fallback for [[maybe_unused]] in C++14.
Definition: tdecl.h:130