based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |

type_traits.cpp (3018B)


1 #include "based/type_traits.hpp" 2 3 namespace 4 { 5 6 struct irregular 7 { 8 irregular() = default; 9 10 irregular(const irregular&) = delete; 11 irregular(irregular&&) = delete; 12 13 irregular& operator=(const irregular&) = delete; 14 irregular& operator=(irregular&&) = delete; 15 16 ~irregular() = default; 17 }; 18 19 struct no_return 20 { 21 void operator()() {} 22 }; 23 24 template<typename T> 25 struct identity 26 { 27 T operator()(T val) { return val; } 28 }; 29 30 template<typename T, typename U> 31 struct add 32 { 33 T operator()(const T& val1, const U& val2) { return val1 + val2; } 34 }; 35 36 template<typename T> 37 struct mutate 38 { 39 T operator()(T* val) { return *val = 1; } 40 }; 41 42 template<typename T, typename U> 43 T sub(T val1, U val2) 44 { 45 return val1 - val2; 46 } 47 48 } // namespace 49 50 int main() 51 { 52 static_assert(based::arity_v<no_return> == 0); 53 static_assert(based::Procedure<no_return>); 54 static_assert(!based::RegularProcedure<no_return>); 55 static_assert(!based::FunctionalProcedure<no_return>); 56 57 using id = identity<double>; 58 using ii = identity<irregular>; 59 60 static_assert(std::same_as<based::domain_t<id>, double>); 61 static_assert(std::same_as<based::codomain_t<id>, double>); 62 static_assert(based::arity_v<id> == 1); 63 64 static_assert(based::Procedure<id>); 65 static_assert(based::Procedure<ii>); 66 67 static_assert(based::RegularProcedure<id>); 68 static_assert(!based::RegularProcedure<ii>); 69 70 static_assert(based::FunctionalProcedure<id>); 71 static_assert(!based::FunctionalProcedure<ii>); 72 73 using ad = add<double, double>; 74 using ai = add<irregular, irregular>; 75 using aid = add<irregular, double>; 76 using adi = add<double, irregular>; 77 78 static_assert(std::same_as<based::domain_t<ad>, double>); 79 static_assert(std::same_as<based::codomain_t<ad>, double>); 80 static_assert(based::arity_v<ad> == 2); 81 82 static_assert(based::Procedure<ad>); 83 static_assert(based::Procedure<ai>); 84 static_assert(based::Procedure<aid>); 85 static_assert(based::Procedure<adi>); 86 87 static_assert(based::RegularProcedure<ad>); 88 static_assert(!based::RegularProcedure<ai>); 89 static_assert(!based::RegularProcedure<aid>); 90 static_assert(!based::RegularProcedure<adi>); 91 92 static_assert(based::FunctionalProcedure<ad>); 93 static_assert(!based::FunctionalProcedure<ai>); 94 static_assert(!based::FunctionalProcedure<aid>); 95 static_assert(!based::FunctionalProcedure<adi>); 96 97 using md = mutate<double>; 98 99 static_assert(based::Procedure<md>); 100 static_assert(based::RegularProcedure<md>); 101 static_assert(!based::FunctionalProcedure<md>); 102 103 static_assert(based::RegularProcedure<decltype(sub<double, double>)>); 104 105 static const auto l1 = [](double a) { return a; }; 106 static_assert(based::Procedure<decltype(l1)>); 107 static_assert(based::RegularProcedure<decltype(l1)>); 108 static_assert(based::FunctionalProcedure<decltype(l1)>); 109 110 static const auto l2 = [](irregular /* a */) { return 1; }; 111 static_assert(based::Procedure<decltype(l2)>); 112 static_assert(!based::RegularProcedure<decltype(l2)>); 113 static_assert(!based::FunctionalProcedure<decltype(l2)>); 114 115 return 0; 116 }