Skip to content

Instantly share code, notes, and snippets.

View picanumber's full-sized avatar

Nikos Athanasiou picanumber

View GitHub Profile
@picanumber
picanumber / sorted_view_outline.cpp
Last active January 11, 2017 11:46
interface of the sorted view class
template <class... Args>
class sorted_view
{
std::tuple<Args...> _ts;
mutable std::vector<std::size_t> _is;
public:
sorted_view(Args&&... args);
// sort based on the Ith sequence
template <std::size_t I, class F> void sort(F&& bop) const;
@picanumber
picanumber / quantify_variadic_predicates.cpp
Last active December 19, 2016 07:56
Quantification for constant typelist - variadic predicate list
template <class T, template <class> class... Ps>
constexpr bool satisfies_all_v = std::conjunction<Ps<T>...>::value;
template <class T, template <class> class... Ps>
constexpr bool satisfies_any_v = std::disjunction<Ps<T>...>::value;
template<typename T> concept bool Data = requires(T t) { t.data; };
template<Data... Ts>
requires existential_quantifier<std::is_move_constructible, Ts...>
auto f(Ts... ts)
{
std::cout << "version for data\n";
return 0;
}
@picanumber
picanumber / concepts_n_quantifiers.cpp
Last active December 21, 2016 19:25
Quantifiers as concepts
// -----------------------------------------------------------
// -------------------- to express "1 predicate Vs many types"
template <template <class> class P, class... Ts>
concept bool existential_quantifier = any_v<P<Ts>::value...>;
template <template <class> class P, class... Ts>
concept bool universal_quantifier = all_v<P<Ts>::value...>;
// -----------------------------------------------------------
// -----------------------------------------------------------
@picanumber
picanumber / con_dis.cpp
Created December 18, 2016 21:48
Implementation of conjunction and disjunction in the Standard library
template<class...> struct conjunction : std::true_type { };
template<class B1> struct conjunction<B1> : B1 { };
template<class B1, class... Bn>
struct conjunction<B1, Bn...>
: std::conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};
template<class...> struct disjunction : std::false_type { };
template<class B1> struct disjunction<B1> : B1 { };
template<class B1, class... Bn>
struct disjunction<B1, Bn...>
@picanumber
picanumber / meta_bind_example.cpp
Last active December 18, 2016 22:22
Example application of metafunction currying
struct S0 {};
struct alignas(2) S1 {};
struct alignas(4) S2 {};
struct alignas(8) S3 {};
int main()
{
static_assert(universal_quantifier<curry<is_smaller, S0>::type, S1, S2, S3>);
}
@picanumber
picanumber / meta_curry.cpp
Last active December 18, 2016 21:03
Bind for metafunctions
template <template <class...> class C, class...Ts>
struct curry
{
template <class T>
using type = C<Ts..., T>;
};
@picanumber
picanumber / is_smaller.cpp
Created December 18, 2016 20:50
Bivariate predicate example
template <class T1, class T2>
using is_smaller = std::integral_constant<bool, sizeof(T1) < sizeof(T2)>;
@picanumber
picanumber / univ_quant_example.cpp
Created December 18, 2016 20:31
Example usage of the universal quantifier
template <class... Ts>
constexpr bool are_tc = universal_quantifier<std::is_trivially_copyable, Ts...>;
@picanumber
picanumber / quantifiers.cpp
Last active December 18, 2016 20:22
Definition of the existential and universal quantifiers
// ------------------------------------ "is there ?"
template <template <class> class P, class... Ts>
constexpr bool existential_quantifier = any_v<P<Ts>::value...>;
// ----------------------------------------- "are all ?"
template <template <class> class P, class... Ts>
constexpr bool universal_quantifier = all_v<P<Ts>::value...>;