Last active
August 17, 2019 22:33
-
-
Save loliGothicK/bf4e987f8431238eae91 to your computer and use it in GitHub Desktop.
C++メタ関数のまとめ ref: https://qiita.com/_EnumHack/items/ee2141ad47915c55d9cb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::is_same<T,U>::value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < class T, T value > | |
class integral_constant; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < typename T > | |
struct is_pointer { | |
static constexpr bool value = false ; | |
} | |
template < typename T > | |
struct is_int<T*> { | |
static constexpr bool value = true ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) {}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) {}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>())) {}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func_1( x, y + 2, z ); | |
func_2( (x--, y + 2), z ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
integral_constant<class T, T value> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) {}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>())) {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < typename T > | |
std::add_rvalue_reference<T> value() ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
{}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < typename ... > | |
using void_t = void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
{}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class,class=void> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class T> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct is_equality_comparable<T, | |
void_t<decltype(std::declval<T&>() == std::declval<T&>() )> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::begin(a), std::end(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typename add_const<T>::type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin(a), end(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < typename T > | |
struct is_range : std::bool_constant< | |
A<T>::value || B<T>::value | |
>{}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
{}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 全ての条件が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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < typename T > | |
struct is_range | |
: std::disjunction< | |
detail::enable_std_begin_end<T>, | |
detail::enable_adl_begin_end<T>> | |
{}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | |
{}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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),""); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static_assert( | |
CRANBERRIES_HAS_TYPE(value_type, std::true_type), // true | |
""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < typename T > | |
using add_const_t = typename add_const<T>::type ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__>{}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ラムダ式は未評価式で使えないのでこれはダメ! | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct protean_bool { | |
constexpr operator std::true_type() const { return{}; } | |
constexpr operator std::false_type() const { return{}; } | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
false ? make_overload(...)(x) : cranberries::protean_bool{}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < typename T, typename U > | |
constexpr bool is_same_v = is_same<T,U>::value ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < typename T > | |
typename T::value_type func(T); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < typename L, typename R > | |
auto plus(L&& l, R&& r) -> decltype(l+r) | |
{ | |
return l + r; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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