tuple의 모든 타입이 arithmetic type 인지 알아내는 템플릿 클래스
최근에 C#으로 코딩좀 하고 있는데…
역시 c++의 강력한 template이 그립다..
개인적으로 OOP형태의 언어에서는 c++의 template 때문에 c++을 버릴 수 없을 것 같다. ㅠ
필요할 것 같아서 작성했는데…
std::is_arithmetic 은 integral type(short, int, __int64 …) 이나 floating-point type(float, double) 이면 true이다.
이걸 tuple의 모든 항목이 true인지 검사하는 템플릿 클래스를 만들어 보았다.
참고로, std::is_arithmetic< std::tuple<…> >::value = false 이다.
template<class Tuple>
struct IsTupleAllArithmetic;
template<class... Args>
struct IsTupleAllArithmetic< std::tuple<Args...> >
{
template<class Tuple>
struct IsInternal;
template<class... Args>
struct IsInternal< std::tuple<Args...> >
{
constexpr bool operator()() const noexcept
{
return operator()(std::integral_constant<size_t, 0>());
}
template<size_t INDEX>
constexpr bool operator()(std::integral_constant<size_t, INDEX>) const noexcept
{
return std::is_arithmetic< std::tuple_element<INDEX, std::tuple<Args...> >::type >::value
&& operator()(std::integral_constant<size_t, INDEX + 1>());
}
template<>
constexpr bool operator()(std::integral_constant<size_t, std::tuple_size< std::tuple<Args...> >::value>) const noexcept
{
return true;
}
};
static constexpr bool value = IsInternal< std::tuple<Args...> >()();
};
assert(std::is_arithmetic< std::tuple<int, __int64, float, double> >::value == false);
assert(IsTupleAllArithmetic< std::tuple<int, __int64, float, double> >::value == true);
assert(IsTupleAllArithmetic< std::tuple<int, __int64*, float, double> >::value == false);
assert(IsTupleAllArithmetic< std::tuple<int, __int64, std::string, double> >::value == false);
응용하면 std::is_integral, std::is_floating_point, std::is_same 등도 만들 수 있다.
VS2015기준으로 작성되어있다.