Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@loliGothicK
Last active August 17, 2019 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loliGothicK/bf4e987f8431238eae91 to your computer and use it in GitHub Desktop.
Save loliGothicK/bf4e987f8431238eae91 to your computer and use it in GitHub Desktop.
std::is_same<T,U>::value
template < class T, T value >
class integral_constant;
template < typename T >
struct is_pointer {
static constexpr bool value = false ;
}
template < typename T >
struct is_int<T*> {
static constexpr bool value = true ;
}
template < typename T >
struct is_vector : std::false_type{};
template < typename T >
struct is_vector<std::vector<T>> : std::true_type {};
template < typename T >
constexpr bool is_vector_v = is_vector<T>::value;
template < typename T >
struct is_tuple : std::false_type{};
template < typename ...Types >
struct is_tuple<std::tuple<Types...>> : std::true_type {};
template < typename T >
constexpr bool is_tuple_v = is_tuple<T>::value;
template <class T>
class has_iterator {
template <class U>
static constexpr bool check(typename U::iterator*)
{ return true; }
template <class U>
static constexpr bool check(...)
{ return false; }
public:
static constexpr bool value = check<T>(nullptr);
};
template <class T>
class has_iterator {
template <class U>
static constexpr std::true_type check(typename U::iterator*);
template <class U>
static constexpr std::false_type check(...);
public:
static constexpr bool value = decltype(check<T>(nullptr))::value;
};
template <class T>
class has_iterator {
template <class U, typename O = typename U::iterator>
static constexpr std::true_type check(int);
template <class U>
static constexpr std::false_type check(long);
public:
static constexpr bool value = decltype(check<T>(0))::value;
};
struct has_iterator_impl {
template <class T>
static std::true_type check(typename T::iterator*);
template <class T>
static std::false_type check(...);
};
template <class T>
class has_iterator :
public decltype(has_iterator_impl::check<T>(nullptr)) {};
struct has_value_impl {
template <class T>
static std::true_type check(decltype(T::value)*);
template <class T>
static std::false_type check(...);
};
template <class T>
class has_value :
public decltype(has_value_impl::check<T>(nullptr)) {};
struct has_f_impl {
template <class T>
static auto check(T&& x)->decltype(x.f(),std::true_type{});
template <class T>
static auto check(...)->std::false_type;
};
template <class T>
class has_f :
public decltype(has_f_impl::check<T>(std::declval<T>())) {};
func_1( x, y + 2, z );
func_2( (x--, y + 2), z );
integral_constant<class T, T value>
struct has_value_impl {
template <class T>
static std::true_type check(decltype(&T::value));
template <class T>
static std::false_type check(...);
};
template <class T>
class has_value :
public decltype(has_value_impl::check<T>(nullptr)) {};
struct is_assignable_impl {
template <class T>
static auto check(T&& x, T const& y) -> decltype(x=y,std::true_type{});
static auto check(...) -> std::false_type;
};
template <class T>
struct is_assignable
: decltype(is_assignable_impl::check(std::declval<T>(),std::declval<T>())) {};
template < typename T >
std::add_rvalue_reference<T> value() ;
template<typename T, typename = void>
struct is_equality_comparable : std::false_type
{};
template<typename T>
struct is_equality_comparable<T,
typename std::enable_if<
true,
decltype(std::declval<T&>() == std::declval<T&>(), (void)0)
>::type
> : std::true_type
{};
template < typename ... >
using void_t = void;
template< class, class=void >
struct is_equality_comparable : std::false_type
{};
template< class T >
struct is_equality_comparable<T,
void_t<decltype(std::declval<T&>() == std::declval<T&>() )>
> : std::true_type
{};
template <class,class=void>
template <class T>
struct is_equality_comparable<T,
void_t<decltype(std::declval<T&>() == std::declval<T&>() )>
std::begin(a), std::end(a)
typename add_const<T>::type
begin(a), end(a)
template < typename T >
struct is_range : std::bool_constant<
A<T>::value || B<T>::value
>{};
#include <type_traits>
#include <utility>
namespace cranberries_magic{
template < class, class=void >
struct enable_std_begin_end : std::false_type {};
template < typename T >
struct enable_std_begin_end<T,
std::void_t<decltype( std::begin(std::declval<const T&>()),std::end(std::declval<const T&>()) )>>
: std::true_type {};
template < class, class=void >
struct enable_adl_begin_end : std::false_type {};
template < typename T >
struct enable_adl_begin_end<T,
std::void_t<decltype( begin(std::declval<const T&>()),end(std::declval<const T&>()) )>>
: std::true_type {};
} // ! namespace cranberries_magic
template < typename T >
struct is_range
: std::bool_constant<
cranberries_magic::enable_std_begin_end<T>::value
|| cranberries_magic::enable_adl_begin_end<T>::value>
{};
template < typename T >
constexpr bool is_range_v = is_range<T>::value;
template < class, class=void >
struct is_iterator : std::false_type {};
template < typename T >
struct is_iterator<T,
std::enable_if_t<
std::is_base_of<std::input_iterator_tag, typename std::iterator_traits<T>::iterator_category>::value
|| std::is_base_of<std::output_iterator_tag, typename std::iterator_traits<T>::iterator_category>::value
>
> : std::true_type
{};
// 全ての条件がtrueなら結果がtrueとなる
constexpr bool result1 = std::conjunction_v<
std::true_type,
std::is_void<void>,
std::is_same<int,int>
>;
// 条件を否定する
constexpr bool result2 = std::negation_v<std::true_type>; // false
template < typename T >
struct is_range
: std::disjunction<
detail::enable_std_begin_end<T>,
detail::enable_adl_begin_end<T>>
{};
template < typename T >
struct is_iterator : std::disjunction<
std::is_base_of<std::input_iterator_tag, typename std::iterator_traits<T>::iterator_category>,
std::is_base_of<std::output_iterator_tag, typename std::iterator_traits<T>::iterator_category>>
{};
#include <type_traits>
#include <utility>
namespace cranberries {
struct protean_bool {
constexpr operator std::true_type() const { return{}; }
constexpr operator std::false_type() const { return{}; }
};
template <class T, class... Ts>
struct Overload : T, Overload<Ts...> {
Overload(T a, Ts... xs): T{a}, Overload<Ts...>{xs...} {}
using T::operator();
using Overload<Ts...>::operator();
};
template <class T> struct Overload<T> : T {
Overload(T a): T{a} {}
using T::operator();
};
template <class... F>
inline constexpr Overload<F...>
make_overload(F&&... f)
{
return {{std::forward<F>(f)}...};
}
template <class T> struct type_placeholder{ using type = T; };
}
#define CRANBERRIES_HAS_TYPE(XXX, ...) \
bool(false \
? ::cranberries::make_overload( \
[](auto x)->decltype(std::declval<typename decltype(x)::type::XXX>(), std::true_type{}) {return{}; }, \
[](...)->std::false_type {return{}; } \
)(::cranberries::type_placeholder<__VA_ARGS__>{}) \
: ::cranberries::protean_bool{})
int main()
{
static_assert(CRANBERRIES_HAS_TYPE(value_type, std::true_type),"");
}
static_assert(
CRANBERRIES_HAS_TYPE(value_type, std::true_type), // true
"");
template <class T, class... Ts>
struct Overload : T, Overload<Ts...> {
Overload(T a, Ts... xs): T{a}, Overload<Ts...>{xs...} {}
using T::operator();
using Overload<Ts...>::operator();
};
template <class T> struct Overload<T> : T {
Overload(T a): T{a} {}
using T::operator();
};
template < typename T >
using add_const_t = typename add_const<T>::type ;
cranberries::make_overload(
[](auto x)->decltype(std::declval<typename decltype(x)::type::XXX>(), std::true_type{}) {return{}; },
[](...)->std::false_type {return{}; }
)(::cranberries::type_placeholder<__VA_ARGS__>{})
// ラムダ式は未評価式で使えないのでこれはダメ!
decltype(
cranberries::make_overload(
[](auto x)->decltype(std::declval<typename decltype(x)::type::XXX>(), std::true_type{}) {return{}; },
[](...)->std::false_type {return{}; }
)(::cranberries::type_placeholder<SOME_TYPE>())
)::value
struct protean_bool {
constexpr operator std::true_type() const { return{}; }
constexpr operator std::false_type() const { return{}; }
};
false ? make_overload(...)(x) : cranberries::protean_bool{};
template < typename T, typename U >
constexpr bool is_same_v = is_same<T,U>::value ;
template <typename From, typename To>
struct is_convertible_basic_impl
{
// 2 つの同名関数を作って
static no_type _m_check(...);
static yes_type _m_check(To);
// 関数の戻り値の型を見る(どっちの関数が使われるかを見る)
static bool value = sizeof( _m_check(From) ) == sizeof(yes_type);
};
template < typename T >
typename T::value_type func(T);
template < typename L, typename R >
auto plus(L&& l, R&& r) -> decltype(l+r)
{
return l + r;
}
template < typename T >
struct is_int {
static constexpr bool value = false ;
}
template < >
struct is_int<int> {
static constexpr bool value = true ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment