Skip to content

Instantly share code, notes, and snippets.

View picanumber's full-sized avatar

Nikos Athanasiou picanumber

View GitHub Profile
namespace detail
{
template <class...> struct integer_range;
template <class T, class Ofst, T... Vals>
struct integer_range<T, Ofst, std::integer_sequence<T, Vals...>>
{
using type = std::integer_sequence<T, (Ofst::value + Vals)...>;
};
}
int main()
{
int i1(1), i2(1);
char c1('o'), c2('k');
auto tp{ std::make_tuple(2., 4., 23, 7, 'x', 'l') };
std::array<char, 4> ar{ { 'a', 'b', 'c', 'd' } };
// 1. read from a slice -----------------------------------
std::tie(i1, i2) = tuple_slice<2, 4>(tp);
@picanumber
picanumber / old_ct_predicate_logic.cpp
Created December 18, 2016 19:45
Express compile time predicate logic with pre C++17 tools
template< bool... Bs >
using bool_sequence = std::integer_sequence< bool, Bs... >;
template< bool... Bs >
using bool_and = std::is_same<
bool_sequence<Bs... >, bool_sequence<(Bs || true)... >>;
template< bool... Bs >
using bool_or = std::integral_constant< bool, !bool_and< !Bs... >::value >;
@picanumber
picanumber / new_pred_logic.cpp
Last active December 18, 2016 19:49
Predicate logic expressed in C++17
template <bool... Bs> constexpr bool all_v = (... && Bs);
template <bool... Bs> constexpr bool any_v = (... || Bs);
@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...>;
@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 / 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 / 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 / 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 / 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...>